Can You Override Private Method in Java ? Example (original) (raw)
No, We can not override the private method in Java, just like we can not override the static method in Java. Like static methods, the private method in Java is also bonded during compile time using static binding by Type information and doesn't depend on what kind of object a particular reference variable is holding. Since method overriding works on dynamic binding, it's not possible to override the private method in Java.
private methods are not even visible to the Child class, they are only visible and accessible in the class on which they are declared. private keyword provides the highest level of Encapsulation in Java.
Though you can hide the private method in Java by declaring another private method with the same name and different method signature.
Overriding a private method in Java? Example
As per the above paragraph, you can not override the private method in Java because it's bonded during compile time using static binding. It's also not visible on sub-class so cannot be overridden but you can hide the method by declaring another private method of the same signature.
You can also see these free Java courses to learn more about such Java Fundamentals.
Now let's test this theory by an example Java program :
/**
*
* Java program to demonstrate that private method can not be overridden in Java.
* This Java program calls both private and non-private methods with child class
* object on the constructor of the parent class.
* Only non-private method of Child class invoked while the private method of
* Parent class is invoked, Which confirms that the private method can not be overridden in Java
* and only be hidden if we declare the 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 methods 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
the private method inside Parent class in Java
the non-private overridden method from Child class
This example has two classes Parent and Child each contains two methods with the same name and same signature, one of them is a private method and the 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 overriding only applies in case of non-private method.
By the way, calling an overridden method from a constructor is considering as bad practice and I have just shown here to demonstrate that we can not override the private method in Java.
Other Java OOP tutorials you may like
- What is Inheritance in Java with Example
- What is Abstraction in Java with Example
- What is Serialization in Java
- What is Synchronization in Java with Example
- What is Polymorphism in Java with Example
Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
Now, its quiz time so one question for? Can we override a static method in Java? Can you explain with a reason?