Lesson 10: Polymorphism & Method Overriding
11 Jul 2026 1 min Swarnil Singhai
Polymorphism lets code work with the parent type while the actual object's overridden method runs — 'the right behavior, chosen at runtime'.
Real-time example
A shipping system calls calculateCost() on any Shipment, whether it's AirShipment or SeaShipment — the caller doesn't care which.
abstract class Shipment {
abstract double calculateCost(double kg);
}
class AirShipment extends Shipment {
double calculateCost(double kg) { return kg * 12.5; }
}
class SeaShipment extends Shipment {
double calculateCost(double kg) { return kg * 3.2; }
}
Shipment s = new AirShipment();
System.out.println(s.calculateCost(20)); // 250.0
What's happening
The variable's declared type is Shipment, but calculateCost runs the actual object's version — dynamic dispatch.
This is why you can add a new shipment type later without touching any code that already calls calculateCost().
Advertisement