All topics
library
beginner

Polymorphism: Compile-Time & Runtime

Distinguish between static (overloading) and dynamic (overriding) polymorphism and know when each applies.

Polymorphism — many forms — is the ability of a single interface to represent different types. Java supports two flavors.

Overloading = same dish in different sizes (you pick at order time). Overriding = franchise locations cooking the same dish differently (depends on which location you visit).

Key Concepts

1
Compile-time polymorphism (static) is achieved through method overloading: multiple methods with the same name but different parameter lists. The compiler resolves which to call based on argument types at compile time.
2
Runtime polymorphism (dynamic) is achieved through method overriding: a subclass provides a specific implementation of an inherited method. The JVM resolves which to call at runtime based on the actual object type, not the reference type.
3
Key distinctions: overloading is resolved by reference type and argument types at compile time. Overriding is resolved by object type at runtime. When both coexist, the compiler first selects the signature (overloading), then the JVM dispatches to the correct implementation (overriding).
4
Method hiding (static methods in subclasses) looks like overriding but isn't — static methods resolve by reference type. You can't use @Override on static methods.