Can Static, Private and Final Methods Be Overridden
"Can we override a static method?" is one of the most common Java interview questions, and the honest answer is "no, but you can write one that looks like an override". A static method with the same signature in a child class hides the parent's method instead of overriding it. The difference shows up the moment you call it through a parent-type variable. Private and final methods have their own reasons for being off limits, and fields behave like static methods, not like overridden ones.
One Reference, Four Answers
public class Hide {
static class Parent {
String name = "parent field";
static String kind() { return "Parent.kind (static)"; }
String who() { return "Parent.who"; }
private String secret() { return "Parent.secret"; }
String callSecret() { return secret(); }
}
static class Child extends Parent {
String name = "child field";
static String kind() { return "Child.kind (static)"; }
@Override String who() { return "Child.who"; }
private String secret() { return "Child.secret"; }
}
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.who());
System.out.println(p.kind());
System.out.println(p.name);
System.out.println(p.callSecret());
}
}Child.who
Parent.kind (static)
parent field
Parent.secretHow it works
p.who(): an instance method, overridden. The object is aChild, soChild.whoruns. This is the only polymorphic line.p.kind(): a static method. Static methods belong to the class, not the object, so the compiler binds the call to the declared typeParent.Child.kindhidesParent.kind; it doesn't override it.p.name: fields are never overridden. The child'snameis a second, separate field that hides the parent's. Through aParentvariable you get the parent's field.p.callSecret():secret()is private, soChildcan't see it and can't override it.Child.secretis an unrelated method that happens to share the name.
Static Methods: Hidden, Not Overridden
Calling a static method through an instance, as p.kind() does, is legal but misleading. Write Parent.kind() or Child.kind() instead, and the behavior becomes obvious.
You also can't mix the two kinds. A static method can't replace an instance method, or the other way round:
class A { static void s() {} void i() {} }
class B extends A {
void s() {}
static void i() {}
}Mix.java:3: error: s() in B cannot override s() in A
void s() {}
^
overridden method is static
Mix.java:4: error: i() in B cannot override i() in A
static void i() {}
^
overriding method is static
2 errorsAnd @Override on a static method is rejected outright: error: static methods cannot be annotated with @Override.
Private and Final Methods
| Method | Can it be overridden? | Why |
|---|---|---|
private | No | The child can't see it. A same-named method in the child is a brand-new method |
final | No | The author forbade it; the compiler reports overridden method is final |
static | No | It belongs to the class; a same-signature one in the child hides it |
Private methods are also bound at compile time: calls to them always reach the class's own method, so a parent's helper can't be swapped out by a subclass. That's why calling private helpers from a constructor is safe, while calling overridable methods from a constructor is risky.
Common Mistakes
Expecting p.kind() to run the child's static method. It follows the declared type Parent.
Redeclaring a field in a subclass. You get two fields with the same name, and which one you read depends on the variable type. Use a method or a constructor argument instead.
Putting @Override on a private or static method. Both are compile errors, because there's nothing to override.
Interview Corner
Can we override static methods in Java?
No. A static method in a subclass with the same signature hides the parent's method; it doesn't override it. Static calls are bound at compile time to the declared type, so Parent p = new Child(); p.staticMethod() runs the parent's version. A static method also can't hide an instance method or be hidden by one; both are compile errors.
Can private or final methods be overridden?
No. Private methods aren't visible to subclasses, so a method with the same name in the child is a separate method, and calls inside the parent still reach the parent's private method. Final methods are explicitly locked against overriding; trying it gives the compile error "overridden method is final".
What is method hiding?
Method hiding happens when a subclass declares a static method with the same signature as a static method in its parent. Which one runs depends on the reference type used in the call, decided at compile time, not on the object. Fields with the same name are hidden in the same way.
Practice
- Easy: Predict the output, then run it:
Child c = new Child(); System.out.println(c.kind()); System.out.println(((Parent) c).kind());using the classes above. - Harder: Replace the redeclared
namefield inChildwith aname()method overridden in each class, and show that it now follows the object.
Key Takeaways
Was this tutorial helpful?