Java Clear Concepts: Can you override private method with Example?

Java Clear Concepts: Can you override private method with Example?

Override private method: This is a very tricky question and is worthy to be answered clearly for a beginner. Let us start with a small code and clear explanation.

First let us see an example on override private method

The above code compiles, executes and prints "From Demo". Observe, in the super class and subclass, we have the same method display() with same signature. As the program is working fine, can we conclude that private method can be overridden in Java. No, it is a very wrong conclusion.

By principle, in Java, a static method or private method cannot be overridden. Let us dig this statement through the above example.

We know a private access specifier cannot be accessed from any other class (even subclass) or put it other way, it is hidden from other classes. The private method is purely for the usage of same class within the class itself. That is, it not possible by composition also. The private methods are not inherited. When it cannot be called (or accessed or inherited) by other class, the subclass display() is in no way connected with that of super class display() method. Compiler thinks, the subclass display() method belongs to Demo itself and is not the case of overriding. For this reason, the above program compiles. How to prove then? Use annotation introduced from JDK 1.5, as follows.

Rewrite the subclass as follows (by keeping the super class as it is) just adding annotation @Override.

@Override is an annotation that tells the compiler there is an equivalent method (here, display()) in super class. Now, as the rule is broken, the compiler raises error. See the screenshot of compiler error message.

override private method

So finally in Java, private methods of super class cannot be overridden (or called or inherited or accessed) by subclass.

Let us make out some points on this topic worthy to think.

  1. It can be assumed that private methods of super class are implicitly final and hidden from sub class.
  2. This rule is considered as not a violation of Encapsulation.
  3. We can definitely write the private method in the subclass which also exists with the same signature in super class.
  4. If allowed, it poses the security risk. Java is well designed.
  5. private method overriding in Java differs from C++.
  6. When a private method (or a private member) is not visible in the subclass, then it does not sense of overriding it.
  7. It is not correct to imply that private members of super class can be overridden by subclass.
  8. Static methods and private methods are binded at compile time by the type information available to compiler and this is known as static binding. Method overriding concept comes in dynamic binding. So, with static binding principle, private methods cannot be overridden.
  9. As private keyword imposes highest level of restrictions on encapsulation, private methods are not allowed to override.

================Would you like to dig more into?=================

1. Java Private variable accessibility (Private access specifier)
2. What is Java private method?
3. Can we override protected method?

Leave a Reply

Your email address will not be published. Required fields are marked *