Question discussion

What is the output for the below code ?class A{ private void printName(){ System.out.println("Value-A"); }}class B extends A{ public void p…

310633Medium 1 reply 677 views
19
What is the output for the below code ?class A{ private void printName(){ System.out.println("Value-A"); }}class B extends A{ public void printName(){ System.out.println("Name-B"); }}public class Test{ public static void main (String[] args){ B b = new B(); b.printName(); }}
  • AValue-A
  • BName-B
  • CValue-A Name-B
  • DCompilation fails - private methods can't be override
  • ENone of these
Correct answer: B

Discussion 1

0
CCommunity
Name-B

You can not override private method , private method is not availabe in subclass . In this case printName() method a class A is not overriding by printName() method of class B. printName() method of class B different method. So you can call printName() method of class B.