Java Tutorial

Polymorphism

Static and dynamic binding, upcasting/downcasting, and method overriding

What is Polymorphism in Java?

Polymorphism (meaning "many forms") is the ability of an object to behave differently depending on its actual type, even when accessed through a general reference. In Java, this is achieved through Method Overriding and high-level parent references.
java
// A generic Animal reference!
Animal a = new Dog(); // A Dog treated as an Animal!
a.speak(); // Output: "Bark!" (NOT "...")

Comparisons: The Two Types of Polymorphism

Wait! Java has two levels of polymorphism:
  1. Static (Compile-time): Method Overloading. The compiler knows exactly WHICH method to call when you write the code.
  2. Dynamic (Run-time): Method Overriding. The JVM figures out WHICH method to call while the program is running (late binding).

Upcasting and Downcasting

  • Upcasting: Treating a child as its parent (Always safe and automatic).
  • Downcasting: Treating a parent as its specific child (Requires a manual cast and can be dangerous!).
java
// Upcasting (Safe)
Animal a = new Dog(); 

// Downcasting (Risky! Check first!)
if (a instanceof Dog) {
    Dog d = (Dog) a;
    d.bark();
}

Benefits of Polymorphism

Wait! Why do we use polymorphism? It allows you to write highly generic, reusable code that can work with any child class.
java
// One method that can feed ANY animal!
public void feed(Animal a) {
    System.out.println("Feeding...");
    a.eat(); // Calls the specific child's 'eat' method!
}

feed(new Dog()); // Runs Dog.eat()
feed(new Cat()); // Runs Cat.eat()

Summary

TypeExamplesDescription
Staticadd(int), add(double)Overloading (Decided at compile-time)
Dynamic@OverrideOverriding (Decided at run-time)
Best ForReusable logic, plugin systems, generic processing
instanceofKeyword used to identify an object's real type
UpcastSafe; automatic conversion to parent
DowncastManual cast back to specific child
BindingHow the JVM links a method call to actual code
Late B.Logic is decided at the "last possible moment"
InterfacePolymorphism also works perfectly with Interfaces!
SecurityPrevents code from breaking when generic items are added
ModernPattern Matching (Java 16+) makes downcasting much cleaner!
PerformanceNegligible overhead for dynamic lookup
HierarchyParent references can "point" to ANY child object
OverrideMust match the name and signature exactly!
StandardCore principle used by all Java Frameworks (Spring, JPA)
DesignAllows for "Liskov Substitution Principle" (SOLID)
IdentityAn object never "forgets" that it's a Dog!
SafetyPrevents "ClassCastException" with careful checking
GenericCombined with Generics, this is the ultimate tool!
ReturnA method can return an Animal, but actually return a Dog
ParameterPass specialized children into generic parent slots
Logic"One interface, many implementations"
AbstractWorks best with Abstract base classes