What will be the output?class A{ int i = 10; public void printValue(){ System.out.print("Value-A"); }}class B extends A{ int i = 12; public void printValue(){ System.out.print("Value-B"); }}public class Test{ public static void main(String args[]){ A a = new B(); a.printValue(); System.out.print(a.i); }}Value-B 11Value-B 10Value-A 10 Value-A 11
Question
What will be the output?class A{ int i = 10; public void printValue(){ System.out.print("Value-A"); }}class B extends A{ int i = 12; public void printValue(){ System.out.print("Value-B"); }}public class Test{ public static void main(String args[]){ A a = new B(); a.printValue(); System.out.print(a.i); }}Value-B 11Value-B 10Value-A 10 Value-A 11
Solution
The output will be "Value-B 10".
Here's the step by step explanation:
-
Class B extends Class A. This means that Class B inherits all the properties and methods of Class A.
-
In the main method, an object 'a' of Class A is created but it is pointing to an object of Class B due to the line "A a = new B();". This is called upcasting.
-
When the method "a.printValue();" is called, it will call the method from Class B, not Class A, because the object 'a' is pointing to Class B. So, "Value-B" will be printed.
-
After that, "a.i" is called. Even though Class B has its own variable 'i', the variable from Class A will be used because the reference is of type A. So, '10' will be printed.
Therefore, the output is "Value-B 10".
Similar Questions
What will be printed after executing following program code?class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; }}class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; }}public class Test{ public static void main(String[] args){ Base b = new Derived(); System.out.pr 10203040
Analyze the following code and choose the correct answer from below.public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { setI(20); // System.out.println("i from B is " + i); } @Override public void setI(int i) { this.i = 3 * i; } }
What will be the output of the following code snippet?public class MyClass { static int value = 10; public void printValue() { System.out.println(value); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.printValue(); }}Question 4Answera.0b.10c.Compilation errord.Runtime exception
Analyze the following code carefully. Please select the one that applies.public class Test { public static void main(String[] args) { A a = new A(); a.print(); }}class A { String s; A(String s) { this.s = s; }void print() { System.out.println(s); }}
What will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options02Compilation Error1
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.