» » Can we override private method in java

  • We can not override private methods in java.
  • The basic inheritance principle is when we are extending a class we can access all non private members of a class so private members are private to that class only we can not access anywhere outside the class if we declare anything as private.
  • Know more information about access specifiers here

  1. Class A{
  2.  
  3.   int a;
  4.  private int b;
  5.  
  6. public void print(){
  7.  
  8. System.out.println("print method of super class A"); 

  9. }
  10.  
  11. private void add(){
  12.  
  13. System.out.println("add method of super class A"); 

  14. }
  15. }
  16. Class B extends A{
  17.  
  18. public void show(){
  19.  
  20.  System.out.println("show method of sub class B"); 

  21. }
  22. public static void main(String[] args){
  23.   
  24. B obj= new B();
  25. obj.a=30;
  26.  
  27. obj.print();
  28. obj.show();

  29.  System.out.println("a="obj.a); 
  30. //obj.b=20;  compile time error. The field Super.a is not visible
  31. //obj.add(); compile time error : The method show() from the type Super is not visible

  32. }
  33. }



Output:
  1. print method of super class  A
  2. show method of sub class B
  3. 30


  • From the above program we can say super class private members are not accessible to sub class.
  • lets see what will happen if we try to override a method of super class?
  • Sorry if we are unable to access super class private methods then there should not be a questions of overriding that method right?
  • Yes private methods of super class can not be overridden in sub class.
  • Even if we try to override same method of super class that will became a sub class own method not overridden method.


  1. Class A{
  2.  
  3.   int a;
  4.  private int b;
  5.  
  6. private void add(){
  7.  
  8. System.out.println("add method of super class A"); 

  9. }
  10. }
  11. Class B extends A{
  12.  
  13. private void add(){
  14.  
  15.  System.out.println("add method of sub class B"); 

  16. }
  17. public static void main(String[] args){
  18.   
  19. B obj= new B();
  20. obj.a=30;
  21.  
  22. obj.add();


  23.  System.out.println("a="obj.a); 

  24. }
  25. }

Output:
  1. add method of sub class B
  2. 30

  • We can prove this by providing @override annotation in sub class method 

  1. Class A{
  2.  
  3.   int a;
  4.  private int b;
  5.  
  6. private void add(){
  7.  
  8. System.out.println("add method of super class A"); 

  9. }
  10. }
  11. Class B extends A{
  12.  //@override
  13. private void add(){// if we place @override annotation compile time error will come here
  14.  
  15.  System.out.println("add method of sub class B"); 

  16. }
  17. public static void main(String[] args){
  18.   
  19. B obj= new B();
  20. obj.a=30;
  21.  
  22. obj.add();


  23.  System.out.println("a="obj.a); 

  24. }
  25. }

Output:
  1. add method of sub class B
  2. 30

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu