Can we override Private Method in Java
In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not.
Override in Java
Any object-oriented programming language has a feature called overriding that enables a subclass or child class to provide a specific implementation of a function that is already provided by one of its super-classes or parent classes. When two methods have the same name, identical parameters or signature, and identical return type, the replacement method is referred to as a "override" method (or sub-type).
Method overriding is one technique Java uses to establish Run Time Polymorphism. The object that calls a method will determine which version of the method is executed. When a method is called from an object of a parent class, the method's parent class version is executed; however, when a method is called from an object of a subclass, the child class version is executed. In other words, the version of an overridden method that is run depends on the type of the object being referenced, not the type of the reference variable.
Private Method
Private methods in Java are those that have the private access modifier, are restricted to access in the defining class only, and are not visible in their descendant class as a result. As a result, they are ineligible to be overridden. The same method can be defined in the child class and accessed in the parent class, though. A method cannot be overridden in any of the classes that are inheriting it when the final specifier is used. Methods are finalized for design purposes.
Private methods are final in Java by default because they cannot be accessed. In light of this, adding a final specifier to a private method is of no benefit. In fact, it might lead to needless misunderstanding.
Can We Override Private Method in Java?
No, with Java we cannot override static or private methods.
Java's private methods are only accessible to the class in which they are declared, limiting their use to that class. Java does not allow overriding of static or private methods.
The super class method will be hidden if an identical method with the same return type and method arguments is created in a child class; this is referred to as method hiding. Like how you cannot access a private method in a subclass, you also cannot override it. What you can do is add a second, identical private method to the child class. Let's look at the example below so that we can better grasp it.
Example:
class Par {
private void disp() {
System.out.println("class is super calss");
}
}
public class Example extends Parent {
void disp() // here trying to override disp() {
System.out.println("Class is sub class");
}
public static void main (String [] s) {
Par ob = new Ex ();
Ob.disp();
}
}
Output:
Ex.java:17: error: disp () has access of private in Parent
obj. method ();
^
1 error
The application displays an error at compilation time indicating that display () in the Parent class has private access and cannot be overridden in the subclass Example.
A method is a member of a class rather than the class's object if it is specified as static. It may be called without first producing a class object. A static method can also access the class's static data elements.
Example 2:
class Par {
static void disp() {
System.out.println("class is a Super class");
}
}
public class Example extends Parent {
void disp () // trying to override display () {
System.out.println("Class is asub class");
}
public static void main (String [] s) {
Par ob = new Ex ();
ob.Disp();
}
}
Output:
Example.java:10: error: display () in Example given cannot override display () in Parent
void display () // trying to override display ()
^
The method overridden is static
1 error
We established an outer class and an inner class in the software. We added a function disp () to both Outer and Inner, which is an extension of Outer. It is obvious that the method disp () has not been overridden if we look at our output.
This is the case because private methods are bound together at compile time, and the method that is called depends on the type of the reference variable rather than the type of the object it refers to. In addition, because of static binding, private methods may perform better than non-private and non-final methods.