Override private methods


Can we override private methods in Java?

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.

 

Let us first consider the following Java program as a simple example of Runtime Polymorphism or Overriding.

class Base {
  public void fun() {
     System.out.println("Base class fun");  
  }
}
  
class Derived extends Base {
  public void fun() {  // overrides the Base's fun()
     System.out.println("Derived class fun"); 
  }
  public static void main(String[] args) {
      Base obj = new Derived();
      obj.fun();
  } 
}

 

The program prints “Derived class fun”.


The Base class reference ‘obj’ refers to a derived class object (see expression “Base obj = new Derived()”). When fun() is called on obj, the call is made according to the type of referred object, not according to the reference.

Is Overriding possible with private methods?

 In Java, methods declared as private can never be overridden, they are in-fact bounded during compile time. 
Following program have complile time error.

class Base {
  private void fun() {
     System.out.println("Base class fun");   
  }
}   

class Derived extends Base {
  private void fun() {
     System.out.println("Derived class fun"); 
  }

  public static void main(String[] args) {
      Base obj = new Derived();
      obj.fun();
  } 
}


An inner class can access private members of its outer class. What if we extend an inner class and create fun() in the inner class?
We get compiler error “fun() has private access in Base Class” . So the compiler tries to call base class function, not derived class, means fun() is not overridden.

An Inner classes can access private members of its outer class, for example in the following program, fun() of Inner accesses private data member msg which is fine by the compiler.

/* Java program to demonstrate whether we can override private method of outer class inside its inner class */

class Outer {
     private String msg = "mindclues";
     private void fun() {
          System.out.println("Outer class fun()");
     }

     class Inner extends Outer {
         private void fun()  {
               System.out.println("Accessing Private Member of Outer class: " + msg);
         }
     }

     public static void main(String args[]){

   // In order to create instance of Inner class, we need an Outer class instance. So, first create       //Outer class instance and then inner class instance.

          Outer o = new Outer();
          Inner  i   = o.new Inner();

           // This will call Inner's fun, the purpose of this call is to show that private members                 of Outer can be accessed in Inner.

          i.fun();  // o.fun() calls Outer's fun (No run-time polymorphism).
          o = i;
          o.fun();

     }
}

Output:

Accessing Private Member of Outer class: mindclues
Outer class fun()

In the above program, we created an outer class and an inner class. We extended Inner from Outer and created a method fun() in both Outer and Inner. If we observe our output, then it is clear that the method fun() has not been overriden. It is so because private methods are bonded during compile time and it is the type of the reference variable – not the type of object that it refers to – that determines what method to be called.. As a side note, private methods may be performance-wise better (compared to non-private and non-final methods) due to static binding.

Related Articles

post a comment