Polymorphism
By the end of this lesson, you'll be able to:
- Explain polymorphism as treating different types uniformly through a shared type
- Loop over a collection of a parent/interface type containing several subclasses
- Recognize why polymorphism reduces the need for type-checking branches
Prerequisites: "Interfaces"
One list, several actual types
List<Shape> shapes = new List<Shape>{
new Circle(),
new Rectangle()
};
for (Shape shape : shapes) {
System.debug(shape.calculateArea());
}
shapes is typed as List<Shape>, but it actually holds a mix of Circle and Rectangle objects. The loop calls calculateArea() on each without ever checking which one it is — each object runs its own overridden version automatically. This is polymorphism: "many forms" behind one shared type.
What this replaces: manual type-checking
// The version WITHOUT polymorphism — fragile and grows worse over time
for (Object shapeObj : untyped) {
if (shapeObj instanceof Circle) {
Circle c = (Circle) shapeObj;
System.debug(Math.PI * c.radius * c.radius);
} else if (shapeObj instanceof Rectangle) {
Rectangle r = (Rectangle) shapeObj;
System.debug(r.width * r.height);
}
// every new shape type means editing this method again
}
Every new shape type would mean coming back to add another else if branch here. With polymorphism, adding a new Shape subclass requires zero changes to this loop — it already knows how to call calculateArea() on anything that's a Shape.
A real business example: Payroll
List<Payable> payees = new List<Payable>{
new Contractor(),
new SalariedEmployee()
};
Decimal totalPayout = 0;
for (Payable payee : payees) {
totalPayout += payee.calculatePayment();
}
A payroll run doesn't need to know or care whether it's paying a contractor or a salaried employee — both fulfil the Payable interface, so one loop handles the entire payroll regardless of how many different payee types exist.
Exercise
Given the Payable interface and Contractor class from the previous lesson, plus a second class SalariedEmployee implementing Payable (returning a fixed 8000), write a loop over a List<Payable> containing one of each, debugging each calculatePayment() result.
Show hint
for (Payable p : payees) { System.debug(p.calculatePayment()); }
Polymorphism Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Polymorphism means code can work with several different types uniformly, as long as they share a parent class or interface — without needing to know or check which specific type it's dealing with.