Java Interview Question: Can We Override Static Or Private Methods?
Answering Popular Java Interview Questions
In Java, we cannot override private or static methods. Here’s why:
𝟏. 𝐏𝐫𝐢𝐯𝐚𝐭𝐞 𝐌𝐞𝐭𝐡𝐨𝐝𝐬
Private Methods are not accessible outside of the class they are defined. Since overriding is a concept related to inheritance and occurs when a subclass provides a specific implementation of a method already defined in a superclass, private methods cannot be overridden because they are not visible to subclasses.
If the subclass defines a method with the same name as the private method in the superclass, it doesn’t override the superclass method. Instead, it defines a new method specific to the subclass, and the superclass method remains separate.
Example:
class SuperClass {
private void display() {
System.out.println("SuperClass display");
}
}
class SubClass extends SuperClass {
private void display() {
System.out.println("SubClass display");
}
}
In the above example, SubClass
defines its own display
method, but it does not override the display
method in SuperClass
. Both methods are independent.
2. Static Methods
The static method belongs to the class rather than an instance of the class. Since overriding is based on polymorphism, which relies on instance methods, static methods cannot be overridden.
If a subclass defines a static method with the same name and signature as one in its superclass, this is called method hiding and not overriding. The method in the subclass hides the one in the superclass but doesn’t override it.
In summary, Private methods cannot be overridden because they are not visible to subclasses and Static methods cannot be overridden but can be hidden in subclasses. The method called is determined by the type of the reference, not the object.
class SuperClass {
static void display() {
System.out.println("SuperClass display");
}
}
class SubClass extends SuperClass {
static void display() {
System.out.println("SubClass display");
}
}
In this example, the display
method in SubClass
hides the display
method in SuperClass
. The method to be called depends on the class reference used, not on the actual object instance:
SuperClass obj = new SubClass();
obj.display(); // Output: SuperClass display
SubClass obj2 = new SubClass();
obj2.display(); // Output: SubClass display
Here, obj.display()
calls the display
method in SuperClass
because display
is a static method and static methods are resolved at compile time based on the reference type (SuperClass
).
Conclusion
Private methods cannot be overridden because they are not visible to subclasses.
Static methods cannot be overridden but can be hidden in subclasses. The method called is determined by the type of the reference, not the object.
If you are interested in sponsoring this newsletter , reach out to surajmishra150893@gmail.com. Basic details can be found here.
Connect with me, LinkedIn | Twitter. If you have any questions related to SWE/Career, etc, don’t hesitate to DM me. I would love to help my fellow SWEs using my 9 years of experience working in fintech across different countries. ( India/Japan/US/Canada )