Can static methods be overridden in Java?
No. A static method in a child with the same signature hides the parent's one. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.
Explain it simply
Static methods belong to the class, not to any object. Dynamic dispatch needs an object to look at, and static calls have none. So the compiler fixes the target while compiling, from the variable's declared type.
Example
class P { static String who() { return "P"; } }
class C extends P { static String who() { return "C"; } } // hiding, not overriding
P p = new C();
System.out.println(p.who()); // prints: P
System.out.println(C.who()); // prints: CAdding @Override on the child static method gives a compile error, which is a handy way to check you are not overriding by accident.
What to say in the interview
"Static methods cannot be overridden. A same-signature static method in a subclass hides the parent's, and calls are bound at compile time using the reference type. @Override on a static method fails to compile."
Common mistakes
- Thinking the output would be C because the object is a C.