πŸ“Š JAVA
Q. 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) Value-A Name-B D. Compilation fails - private methods can't be override
πŸ’¬ Discuss
βœ… Correct Answer: (B) Name-B

Explanation: 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.

Explanation by: Ranjeet
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
135
Total Visits
πŸ“½οΈ
1 y ago
Published
πŸŽ–οΈ
Ranjeet
Publisher
πŸ“ˆ
86%
Success Rate