e correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }
Question
e correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }
Solution
The code will not compile. The method output(int x, int y) in the Program class is attempting to override the output(int x, int y) method in the Codetantra class. However, the output(int x, int y) method in the Codetantra class is declared as final, which means it cannot be overridden.
Additionally, the variable y is not defined in the main method. If you meant to call the output method on the object instance of Program, you should replace y.output(0, 1) with object.output(0, 1).
So, the correct code should be:
class Codetantra {
final public int output(int x, int y) { return 1; }
}
class Program extends Codetantra {
// This method cannot override the final method from Codetantra
// public int output(int x, int y) { return 2; }
}
public class result {
public static void main(String args[]) {
Program object = new Program();
System.out.print("y is " + object.output(0, 1));
}
}
This will compile and run, printing "y is 1" to the console.
Similar Questions
Select the correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }Optionsy is : 0y is : 1Compilation ErrorAn exception is thrown at runtime
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
the correct answerWhat will be the output of the given Java program? abstract class java { int x; abstract void display(); } class method extends java { int y; void display() { System.out.println(y); } } class Abstract_demo { public static void main(String args[]) { method obj = new method(); obj.y = 1; obj.display(); } }Options0Runtime Error1Compilation Error
What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world"; String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}
What will be the output of the following Java program?class java { public static void main(String args[]) { int var1 = 3; int var2 = 8; if ((var2 = 2) == var1) System.out.print(var2); else System.out.print(++var2); } }Options3421
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.