The Artima Developer Community
Sponsored Link

Java Buzz Forum
Can we override private method in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Can we override private method in java Posted: Jul 12, 2015 7:03 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Can we override private method in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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

Read: Can we override private method in java

Topic: Workshop in a Can: Setup a JBoss BPM Suite Full Day Workshop Previous Topic   Next Topic Topic: The Evolution of Database Schemas using SQL + NoSQL

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use