What is Method Hiding in OOP?
Method hiding vs overriding in Java — why static methods resolve by reference type, not runtime object — with a clear code example.
Expected Interview Answer
Method hiding occurs when a subclass defines a static method with the same signature as a static method in its superclass, so the method that runs is chosen at compile time based on the reference type, not the object’s actual runtime type, unlike true overriding of instance methods.
Because static methods belong to the class itself rather than to any instance, they cannot participate in dynamic dispatch, so calling a static method through a superclass-typed reference always invokes the superclass version, even if the reference actually points to a subclass object. This looks superficially like overriding because the method name and signature match, but the resolution mechanism is entirely different: it is resolved by the compiler using the declared (static) type of the reference, not the object at runtime. The same concept applies to hiding static fields and to shadowing instance fields (fields are never polymorphic in Java). Method hiding is a common interview trap because code that appears to demonstrate polymorphism with static methods actually silently falls back to compile-time resolution.
- Clarifies why static methods cannot be truly polymorphic
- Explains unexpected output when static methods are called via base-type references
- Distinguishes compile-time (static) binding from runtime (dynamic) binding
- Helps avoid a classic interview and code-review pitfall
AI Mentor Explanation
A cricket board publishes a fixed rulebook clause under the 'Federation' letterhead, and a national association publishes its own version of that same clause under its own letterhead, but which clause a printed program shows depends purely on which letterhead was printed on the cover, not on which body actually organized the match. Even if the national association is running the game, a program printed with the Federation’s letterhead shows the Federation’s clause. That is method hiding: which version prints is decided by the declared letterhead (reference type) at compile time, not by who is actually in charge (the runtime object).
Step-by-Step Explanation
Step 1
Define a static method in the superclass
e.g. static void identify() in class Base.
Step 2
Redefine the same static signature in the subclass
class Derived extends Base also declares static void identify().
Step 3
Call through a base-typed reference
Base ref = new Derived(); ref.identify() calls Base.identify(), resolved by the reference type.
Step 4
Contrast with instance method overriding
The same setup with instance methods would call Derived's version via dynamic dispatch instead.
What Interviewer Expects
- A correct distinction between method hiding (static) and true overriding (instance)
- Explanation that static methods are resolved by reference type at compile time
- A code example showing surprising output when called via a base-typed reference
- Awareness that fields are also never polymorphic (shadowed, not overridden)
Common Mistakes
- Calling method hiding "overriding of static methods" (static methods cannot be truly overridden)
- Assuming @Override compiles the same way for static methods as instance methods
- Not knowing the resolution uses the declared/reference type, not the runtime object type
- Forgetting that instance field access is also resolved statically, not polymorphically
Best Answer (HR Friendly)
“Method hiding happens when a subclass defines a static method with the same signature as one in its parent class. Unlike overriding an instance method, which is resolved at runtime based on the actual object, a static method call is resolved at compile time based on the type of the reference you are using. So calling it through a parent-typed reference always runs the parent’s version, even if the object underneath is actually the subclass, which can be a surprising gotcha.”
Code Example
class Base {
static void staticGreet() { System.out.println("Base static greet"); }
void instanceGreet() { System.out.println("Base instance greet"); }
}
class Derived extends Base {
static void staticGreet() { System.out.println("Derived static greet"); } // hides, doesn’t override
@Override
void instanceGreet() { System.out.println("Derived instance greet"); } // true override
}
Base ref = new Derived();
ref.staticGreet(); // prints "Base static greet" — resolved by reference type
ref.instanceGreet(); // prints "Derived instance greet" — resolved by actual object at runtimeFollow-up Questions
- Why can static methods not be overridden in Java?
- How is field shadowing similar to method hiding?
- What compiler warning, if any, would flag a hidden static method?
- Would using @Override on a hiding static method compile?
MCQ Practice
1. Method hiding applies specifically to?
Method hiding occurs when a subclass redefines a static method with the same signature as the superclass.
2. A hidden static method call through a superclass-typed reference resolves based on?
Static method calls are resolved at compile time using the declared type of the reference, not the runtime object.
3. Which statement about method hiding vs overriding is correct?
Method hiding is resolved statically at compile time, while true overriding of instance methods uses runtime dynamic dispatch.
Flash Cards
Method hiding in one line? — A subclass redefines a static method with the same signature as the superclass; resolved at compile time.
How is it resolved? — By the declared (reference) type at compile time, not the actual runtime object.
How does it differ from overriding? — Overriding (instance methods) uses runtime dynamic dispatch; hiding (static methods) does not.
What else is never polymorphic? — Instance fields — accessing a field is also resolved by the declared type, i.e. shadowing.