Polymorphism
Polymorphism means the same call can run different code depending on the object's real type. You write against a general type; the JVM picks the matching implementation.
Detailed Explanation
You want one loop that prints every shape. Circles and rectangles compute area differently. If the loop names Circle and Rectangle explicitly, every new shape edits the loop. Polymorphism flips that: the loop holds a Shape and calls area(). Each object already knows how to answer.
Java does this with subtype polymorphism. A variable's compile-time type decides what you may call. The object's runtime class decides which body runs. That lookup is dynamic dispatch. It applies to instance methods that are not static, private, or final in a way that prevents overriding. It does not apply to fields. Field access uses the compile-time type, which surprises people who expect fields to override.
Interfaces are the usual handle. List is the type you declare. ArrayList or LinkedList is what you construct. Callers stay stable when you swap the implementation. Abstract classes play the same role when implementations share code. Overloading is not this: choosing print(int) versus print(String) happens at compile time from the argument types. That is static polymorphism in textbooks, and it is a different mechanism.
You reach for it when several types share a contract and callers should not switch on class names. Payment processors, notification channels, and persistence adapters are everyday cases. You avoid it when there is only one implementation and no likely second, or when the variants differ only in data. A record plus a single method can be enough.
The Liskov substitution idea is the safety rule. Any code that works with the parent type must keep working when you pass a child. A Square that throws when setWidth is called without setHeight is not a substitutable Rectangle. The type system cannot check that. Your design has to.
The cost is indirection. A virtual call is cheap on HotSpot after inlining, but a megamorphic call site (many runtime types) is harder to inline. The larger cost is conceptual: readers must find the implementation that actually ran. Keep the interface small, name methods after outcomes (area, send), and do not hide control flow behind twenty tiny overrides.
Code Examples
interface Shape {
int area();
}
class Rectangle implements Shape {
private final int w, h;
Rectangle(int w, int h) {
this.w = w;
this.h = h;
}
@Override
public int area() {
return w * h;
}
}
class Square implements Shape {
private final int side;
Square(int side) {
this.side = side;
}
@Override
public int area() {
return side * side;
}
}
public class Areas {
static int total(Shape[] shapes) {
int sum = 0;
for (Shape s : shapes) {
sum += s.area(); // runtime type picks Rectangle or Square
}
return sum;
}
public static void main(String[] args) {
Shape[] shapes = { new Rectangle(3, 4), new Square(5) };
System.out.println(total(shapes)); // prints: 37
}
}class Parent {
String label = "parent";
String name() {
return "parent";
}
}
class Child extends Parent {
String label = "child"; // this field hides, it does not override
@Override
String name() {
return "child";
}
}
public class FieldsDoNotDispatch {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name()); // prints: child
System.out.println(p.label); // prints: parent
}
}Common Gotchas
Fields do not override
a subclass field with the same name hides the parent field. Methods dispatch; fields do not.
Overloading is chosen at compile time
`handle(Object)` vs `handle(String)` uses the variable's declared type, not the runtime type.
`static` methods do not dispatch
calling `Parent.util()` through a `Child` reference still runs `Parent.util` if you wrote `Parent.util()`.
Breaking substitution
throwing `UnsupportedOperationException` from an override of a method callers must use makes the subtype a trap.