Question discussion

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…

313890Medium 1 reply 594 views
0
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); }}
  • AValue-B 11
  • BValue-B 10
  • CValue-A 10
  • DValue-A 11
  • ENone of these
Correct answer: B

Discussion 1

0
CCommunity
Value-B 10

If you create object of subclass with reference of super class like ( A a = new B();) then subclass method and super class variable will be executed.