Java Reference ยท Object-Oriented Concepts
Polymorphism in Java
How Java chooses which method runs: static type versus runtime type, dynamic dispatch, interfaces, and why fields and static methods are not polymorphic.
Polymorphism means that one expression can work with objects of many different classes. In Java the most important form is subtype polymorphism: code written against a superclass or interface type calls a method, and the version that actually runs depends on the real class of the object at that moment. The caller does not need to know, or care, which concrete class it holds.
The mechanism behind it is dynamic dispatch. The compiler checks that a method exists on the declared (static) type, but the JVM picks the implementation using the runtime type of the object. This lets you add a new class later without touching the code that uses the old ones.
Shape s = new Circle(2.0); // static type Shape, runtime type Circle double a = s.area(); // runs Circle.area()
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
How it works
| Concept | Decided by | When |
|---|---|---|
| Which method names and signatures are allowed | Static (declared) type | Compile time |
| Which overload is chosen | Static types of the arguments | Compile time |
| Which override runs | Runtime class of the receiver | Run time |
| Field access | Static type of the expression | Compile time |
static method call |
The class (or static type) named | Compile time |
Because only instance methods are dispatched dynamically, a field with the same name in a subclass merely hides the parent's field. Keep fields private and use methods, and the question never arises.
Usage notes
- Program to the interface. Declare variables, parameters and return types with the most general type that offers what you need (
List, notArrayList). - Casting changes the static type, not the object.
((Dog) a).namereads theDogfield, buta.sound()runsDog.sound()either way. A wrong downcast throwsClassCastException; guard it withinstanceofpattern matching. - Overloading is not polymorphism in this sense. Overloads are resolved at compile time from the static argument types. See overloading.
private,staticandfinalmethods are not overridden, so they are never dispatched dynamically.- Constructors calling overridable methods are dangerous. The subclass override runs before the subclass fields are initialized. See initialization order.
- Cost: a virtual call is slightly more expensive than a direct call, but the JIT compiler inlines calls that always see the same class, so it is rarely a concern.
- Parametric polymorphism is the other kind: generics let one class or method work for many types (generic classes and methods).
- Sealed hierarchies with
switchpatterns offer a closed alternative where the compiler checks you covered every case (sealed classes).