Compile-Time vs Runtime Polymorphism in Java
Fast recall
Quick revision
Core answer
Animal a = new Dog(); a.speak() → calls Dog's version. static methods are hidden not overridden — no vtable lookup, reference type wins.Whether the candidate can clearly distinguish method overloading (compile-time, resolved by parameter types) from method overriding (runtime, resolved by actual object type via vtable dispatch). Whether they understand dynamic method dispatch
Thinking overloading is polymorphism in the same sense as overriding. Not understanding that the compiler chooses the overloaded method based on the declared reference type, not the actual object type. Confusing method hiding (static) with overriding (instance)
Explain the JVM mechanism — vtable dispatch for overriding vs simple signature matching at compile time for overloading. Mention that overloading resolution uses the reference type, which leads to surprising behavior with inheritance
Interview-ready guide
Interview answer
Polymorphism in Java means the same method call can produce different behavior depending on the object it's called on. Java achieves this two ways: overloading at compile time, and overriding at runtime. Most of what interviewers call "polymorphism" refers to the runtime kind — the mechanism that makes interface-based design actually work.
Compile-time polymorphism — overloading:
- Overloading lets you define multiple methods with the same name but different parameter types — the compiler resolves which version to call based on the declared argument types at the call site
- The key point is that the reference type decides, not the actual object: passing
Animal a = new Dog()to an overloaded method runs theAnimalversion, because the compiler locked in its choice before the program ran - Overloading is a naming convenience — it keeps your API clean by letting you write
draw(Circle)anddraw(Rectangle)instead of two different method names; it doesn't provide architectural flexibility
Runtime polymorphism — overriding:
- When you call an overridden method, the JVM uses a per-class virtual method table (vtable) to dispatch to the actual object's implementation — not the reference type's version
Animal a = new Dog(); a.speak()callsDog.speak()— the JVM checks Dog's vtable at execution time, even though the reference is declared asAnimal- This is what makes
Listwork: you iterate and callshape.draw()on each element, and the JVM routes toCircle.draw(),Rectangle.draw(), or whatever the real object is — noinstanceofchecks needed
The static method trap:
- Static methods are hidden, not overridden — they bypass the vtable entirely, so the declared reference type decides which version runs, not the actual object
- This is the standard interview trick: both parent and child define
static void display()— calling it via anAnimalreference always runsAnimal.display()even when the object is aDog; the object type is irrelevant
I recommend writing @Override on every intended override — without it, a mistyped method signature silently creates a new method or hides a static method instead of overriding, and the compiler gives no warning.
Compile-Time vs Runtime Polymorphism in Java
Direct Answer
Compile-time polymorphism = method overloading — compiler picks the right method by parameter types at the call site (reference type decides). Runtime polymorphism = method overriding — JVM picks by actual object type at execution via vtable dispatch (object type decides). Animal a = new Dog(); a.speak() → calls Dog's version. static methods are hidden not overridden — no vtable lookup, reference type wins.
Common Mistakes & Pitfalls
Thinking overloading is polymorphism in the same sense as overriding. Not understanding that the compiler chooses the overloaded method based on the declared reference type, not the actual object type. Confusing method hiding (static) with overriding (instance)
Check your understanding
Answer these without looking back at the article.
- What is dynamic method dispatch and how does the JVM's vtable mechanism work?
- If you pass a Dog reference typed as Animal to an overloaded method, which version runs — and why?
- Can you override a static method in Java? What actually happens when you define the same static method in a subclass?
- What is covariant return type in method overriding and when does it apply?
- How does runtime polymorphism enable the open/closed principle in practice?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.