0/0
OOP Principles

Principles of OOP: Polymorphism

Explore the power of "one name, many forms" β€” from compile-time method overloading and constructor chains to runtime dynamic dispatch, safe casting, and the final keyword that locks it all down.

Explore Topics

1
πŸ”„ Method Overloading
One name, many parameter signatures β€” resolved at compile time
πŸ”„
Method Overloading
One name, many parameter signatures β€” resolved at compile time

Polymorphism means "one name with many forms." Compile-time polymorphism (static binding) occurs when the compiler determines which method to call based on the method signature at compile time. Method overloading lets a class define multiple methods with the same name but different parameter lists β€” varying in number, type, or order of parameters. The compiler matches the call to the correct overload during compilation, so there is zero runtime overhead.

πŸ” Binding Resolver β€” Compile-Time vs Runtime
calc.add(10, 20)
Compile-Time Track
Reference: Calculator
Checks method signature against reference type's methods
Runtime Track
Actual Object: Calculator
JVM checks actual object type for method dispatch
Key Ideas
  • Polymorphism = "poly" (many) + "morph" (forms)
  • Method overloading = same name, different parameter signatures
  • Parameters can vary in number, type, or order
  • Resolved at COMPILE TIME (static binding / early binding)
  • No runtime overhead β€” the JVM knows exactly which method to call
  • Return type alone does NOT make a valid overload
Strengths
  • ✦Clean API β€” one intuitive method name for related operations
  • ✦Type safety enforced at compile time β€” no guessing at runtime
  • ✦Zero performance overhead compared to dynamic dispatch
  • ✦Enables fluent APIs and builder patterns
Weaknesses
  • β–³Too many overloads can confuse callers (which one will be called?)
  • β–³Auto-widening (intβ†’double) can cause unexpected overload resolution
  • β–³Cannot overload by return type alone β€” compiler can't disambiguate
  • β–³Default parameter values (like in Kotlin/Python) often cleaner than many overloads
Used in:
REAL-WORLD System.out.println() has 10 overloads (int, double, String, Object...). String.valueOf() similarly overloads for every primitive type.

Code Examples

Each topic has a self-contained Java example. Switch between topics above, then click the Code tab to explore line-by-line.

Calculator β€” Method Overloading
Calculator class defines multiple add() methods. First overload: two ints.

Quiz

Method Overloading Quiz
QUIZ 1/10
Which of these is NOT a valid way to overload a method?
Signature Rules Quiz
QUIZ 2/10
What is a method signature in Java?
Constructor Overloading Quiz
QUIZ 3/10
What must be true about this() in a constructor?
Method Overriding Quiz
QUIZ 4/10
Animal a = new Dog();
a.speak();
a.breathe();
What does this code print?
Covariant Returns Quiz
QUIZ 5/10
If a parent method returns Animal, which return type is valid for an overriding method?
Static Method Hiding Quiz
QUIZ 6/10
Parent ref = new Child();
ref.greet();      // static method
ref.introduce();  // instance method
What does this code print?
Private Method Rules Quiz
QUIZ 7/10
What happens if a child class defines a method with the same signature as a parent's private method?
Upcasting Quiz
QUIZ 8/10
Vehicle v = new ElectricCar();
v.start();
What does this code print?
Downcasting Quiz
QUIZ 9/10
When does a downcast throw ClassCastException?
The final Keyword Quiz
QUIZ 10/10
Why can't constructors be declared final?

Diagrams

Loading diagram...
Loading diagram...
Loading diagram...

Practice Exercises

Classify each concept as Compile-Time or Runtime polymorphism:
EXERCISE 1/3
Method overloading
Method overriding
Constructor overloading
Dynamic method dispatch
Static binding
Auto-widening resolution
Compile-Time (Static Binding)
Runtime (Dynamic Binding)
For each scenario, determine the outcome:
EXERCISE 2/3
Vehicle v = new Car()
(Car) vehicleRef β€” actual is Car
(Car) vehicleRef β€” actual is Bus
parentRef.staticMethod()
parentRef.instanceMethod()
Override a final method
Always Safe / Succeeds
Uses Reference Type
Uses Actual Object Type
Error (Compile or Runtime)
Match each method resolution mechanism to its description:
EXERCISE 3/3
Method Overloading
Method Overriding
Static Method Hiding
Private Method Binding
Compiler picks by parameter signature
JVM picks by actual object type at runtime
Compiler picks by reference type (static)
Compiler picks by declaring class (invisible to children)

References

πŸ“‹

Oracle Java Tutorials β€” Polymorphism

docs.oracle.com

docs
πŸ“š

Effective Java, 3rd Edition β€” Joshua Bloch (Item 52: Use Overloading Judiciously)

oreilly.com

book
πŸ“„

Baeldung β€” Polymorphism in Java

baeldung.com

article
πŸ“‹

Java Language Specification β€” Method Invocation (Β§15.12)

docs.oracle.com

docs
πŸ“š

Head First Object-Oriented Analysis and Design β€” Chapter on Polymorphism

oreilly.com

book