Polymorphism in Java means that one name or one reference type can behave in many ways. There are two kinds: compile-time polymorphism, achieved by method overloading and resolved by the compiler, and runtime polymorphism, achieved by method overriding and resolved by the JVM according to the actual object type.
This guide explains both kinds with runnable examples, shows how dynamic dispatch picks a method, and lists the traps, such as fields and static methods not being polymorphic. Code targets Java 21 and Java 25 LTS.
The word comes from Greek for many forms. In Java it shows up in two places.
| Aspect | Compile-time | Runtime |
|---|---|---|
| Mechanism | Method overloading | Method overriding |
| Also called | Static binding, early binding | Dynamic dispatch, late binding |
| Decided by | Compiler, from argument types | JVM, from the object's real class |
| Needs inheritance | No | Yes (or an interface) |
Overloading means several methods share a name but differ in the number or types of parameters. The compiler picks one by looking at the arguments you pass.
A different return type alone does not make an overload; the parameter list must differ. When no exact match exists, Java widens primitives (int to long to double) or boxes them, which can sometimes pick a method you did not expect, so keep overloads clearly distinct.
When a subclass overrides a method, a call through a parent reference runs the child version. The JVM chooses the method at runtime using the real class of the object, not the declared type of the variable.
The loop code knows only Shape, yet each call runs the right area(). You can add Triangle later without changing the loop. This is the main benefit: code written against a general type works with every future subtype.
Assigning a child to a parent variable is upcasting and is always safe. Going back down needs a cast and may fail. Pattern matching for instanceof (standard since Java 16) makes it safe and short.
Since Java 21 you can also use pattern matching in switch, which works well with sealed hierarchies:
Casting to the wrong type throws ClassCastException, so prefer pattern matching over blind casts.
Dynamic dispatch applies to instance methods only. Three things surprise beginners:
private and final methods cannot be overridden, so no dynamic dispatch happens for them.Overloading is also decided by the declared type of the argument. Calling print(Object) with a String held in an Object variable picks the Object overload.
Overloading is several methods with the same name and different parameters, chosen at compile time. Overriding is a subclass replacing a parent method with the same signature, chosen at runtime.
No. Constructors are not inherited, so they cannot be overridden. You can overload them with different parameter lists.
It is the JVM process of selecting which overridden method to run based on the real class of the object at the moment of the call.
Yes. A variable of an interface type can hold any implementing object, and calls run the implementation of that object's class.
Compile-time polymorphism is overloading, resolved by the compiler from argument types. Runtime polymorphism is overriding, resolved by the JVM from the object's actual class. Program against general types such as abstract classes and interfaces, and remember that fields, static methods and private methods do not dispatch dynamically.