Polymorphism with Static Methods
1. Shocking Truth (beginner!)
Static methods DON'T override — they hide (like fields!). Which version runs depends on REFERENCE (not object!). No runtime magic for static!
2. Proof Code (compare with instance!)
class Animal {
static void show() { System.out.println("Animal static"); }
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
static void show() { System.out.println("Dog static"); } // HIDES (not override!)
@Override void sound() { System.out.println("bark"); } // OVERRIDES!
}
Animal a = new Dog();
a.show(); // Animal static! (reference wins — NO polymorphism!)
a.sound(); // bark! (object wins — true polymorphism!)
// @Override on static → ERROR! (proves no override!)
3. Rules (exam list!)
| Static method | Instance method | |
|---|---|---|
| Child same signature | hides (reference!) | overrides (object!) |
@Override |
ERROR! | OK! |
| Call style | ClassName.m() (preferred!) |
obj.m() |
4. Why? (one line for viva!)
Static belongs to class (no this/object!) — JVM can't dispatch by object. Compiler binds by reference at compile time.
5. Exam Points
- Static-hide vs instance-override trace — 5-7 marks, very frequent.
@Overrideerror on static — 3 marks, favourite proof.