Skip to main content
PreparationZone Logo
PrepZone

Polymorphism

Overloading resolves at compile time, overriding at run time — and method hiding fools everyone once.

Pass checkpoint below

Why this matters

  • Nearly every framework you will use — Spring, JDBC, the Collections Framework — works because of run-time overriding.
  • The "which method runs" puzzle is a staple interview question, and the answer requires separating the two mechanisms cleanly.
  • Static method hiding catches almost everyone once, and knowing it exists saves an afternoon.

Compile-time: overloading

Same name, different parameter lists. The compiler decides, using only the declared types at the call site.

Java
Loading compiler…

The object is a String, but the variable is declared Object, and that is all the compiler looks at. Overloading is never influenced by run-time type.

Run-time: overriding

Same signature, redefined in a subclass. The JVM decides, using the actual object.

Java
Loading compiler…
Overloading — compile time
print(int)Chosen by argument type
print(String)Same name, different signature

Decided before the program runs.

Overriding — run time
Payment p = new UpiPayment()Declared type: Payment
UpiPayment.settle()Real object wins
The compiler picks between overloads from the declared types. The JVM picks between overrides from the real object, which is why a Payment variable can run CardPayment code.

This is dynamic dispatch. The JVM consults the object's class to find the right method body — conceptually a lookup in a per-class method table. It is the mechanism that makes abstraction useful at run time.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 5 sections

  • The two side by side
  • Rules for a valid override
  • Static method hiding
  • Upcasting and downcasting
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.