Examveda
Examveda

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();
      }
}

A. Value-A

B. Name-B

C. Value-A Name-B

D. Compilation fails - private methods can't be override

E. None of these

Answer: Option B

Solution(By Examveda Team)

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 the printName() method of class B. printName() method of class B is a different method. So you can call printName() method of class B.


This Question Belongs to Java Program >> Inheritence

Join The Discussion

Related Questions on Inheritence

What is inheritance in Java?

A. The process of acquiring properties and behaviors of one class by another

B. The process of creating objects

C. The process of encapsulation

D. The process of overloading methods