Can we override private method in Java - Example program

No, We can not override private method in Java, just like we can not override static method in Java. Like static methods, private method in Java is also bonded during compile time using static binding by Type information and doesn't depends on what kind of object a particular reference variable is holding. Since method overriding works on dynamic binding, its not possible to override private method in Java. private methods are not even visible to Child class, they are only visible and accessible in the class on which they are declared. private keyword provides highest level of Encapsulation in Java. Though you can hide private method in Java by declaring another private method with same name and different method signature.



Overriding private method in Java - Testing

As per above paragraph we can not override private method in Java because its bonded during compile time using static binding. Now let's test this theory by an example Java program :

Can private method be overriden in Java /**
 *
 * Java program to demonstrate that private method can not be overridden in Java.
 * This Java programs calls both private and non private method with child class
 * object on constructor of parent class.
 * Only non private method of Child class invoked while private method of
 * Parent class is invoked, Which confirms that private method can not be overridden in Java
 *  and only be hidden if we declare same message in Child class.
 * @author
 */

public class PrivateMethodCanNotBeOverriden{
 
    public static void main(String args[]) {
        //shows that private method can not be overridden in Java    
        Parent parent = new Child();
    }
 
 
}

class Parent{
 
    public Parent(){
        name();
        normal();
    }
 
    private void name(){
        System.out.printf("private method inside Parent class in Java %n");
    }
 
    public void normal(){
        System.out.println("non private method from Parent class can be overridden");
    }
 
}

class Child extends Parent{
 
    /*
     * Private method can not be overridden in Java, they can only be hidden
     */

    private void name(){
        System.out.printf("private method inside Child class in Java %n");
    }
 
    @Override
    public void normal(){
        System.out.println("non private overridden method from Child class ");
    }
 
}

Output
private method inside Parent class in Java
non private overridden method from Child class


This example has two class Parent and Child each contains two method with same name and same signtuare, one of them is private method and other is non private, public in this case. On constructor of Parent class we call both private and non private method and Output shows that overridding only applies in case of non private method. By the way calling overriden method from constructor is considering as bad practice and I have just shown here to demonstrate that we can not override private method in Java.

Other Java OOPS tutorials you may like

12 comments:

  1. Good points :
    Private method is not inherited by subclass in Java. Sub class can not even see private methods of super class. when you declare private method in subclass no body knows about it except code in that class. That's why term method hiding is used. Accessing private method outside class is compile time error in Java.

    ReplyDelete
    Replies
    1. That's true.. It adds some more information to this article.. :)

      Delete
  2. Good this article is very useful for me

    ReplyDelete
  3. Useful information.
    Thanks!

    ReplyDelete
  4. Can we override a private method from super class into subclass? is that different than creating private method with same name in both super class and sub class in Java?

    ReplyDelete
  5. Good Article, asked in two interviews consecutively.

    ReplyDelete
  6. "Can we override a private method from super class into subclass? is that different than creating private method with same name in both super class and sub class in Java?"
    -> Thats a compile time error. compiler wont find anything to override since it cant see the private method

    ReplyDelete
    Replies
    1. @mugume, I don't think that's true. You can see in my example, I have same method name() in both super class and sub class. It's actually an example of method hiding than overloading. If a method is marked private, compiler won't bother to check super class.

      Delete
    2. that error will only occur it you use
      @Override annotation

      Delete
  7. Hello In this Program you are not getting any compile time error if you want you can check.@mugume david

    public class Prgoram2 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Derived d=new Derived();


    }

    }
    class BaseClass
    {
    private void m1()
    {

    }
    }
    class Derived extends BaseClass
    {
    private void m1()
    {

    }
    }

    ReplyDelete
  8. You have cretaed object only in above program....didn't invoke any method.

    ReplyDelete
    Replies
    1. @Sujit, since methods are invoked from constructor, they will automatically be called when you create an object of the class. You don't need to invoke any method.

      Delete