Two subclasses, one superclass, polymorphic loop.
The classic AP scenario: a single array of superclass references holds multiple subclass objects — and one method call produces different results for each.
The full power of polymorphism appears when two or more subclasses share a parent and one piece of client code processes all of them uniformly. The array holds them as the parent type; each call dispatches to the right override.
This is the foundation of the AP CSA "hierarchy of shapes / vehicles / animals" FRQ pattern — and the cleanest demonstration of why polymorphism matters.
On the AP exam, this appears in MCQs about dispatch and in FRQs that ask you to write a generic processing loop over a mixed array.
An array of the parent type; the right method runs per object.
No instanceof checks needed.
Setup
public class Shape {
public double area() { return 0; }
}
public class Circle extends Shape {
private double r;
public Circle(double r) { this.r = r; }
@Override public double area() { return Math.PI * r * r; }
}
public class Square extends Shape {
private double s;
public Square(double s) { this.s = s; }
@Override public double area() { return s * s; }
}
The polymorphic processor
public static double totalArea(Shape[] shapes) {
double total = 0;
for (Shape sh : shapes) {
total += sh.area();
}
return total;
}
One loop. Each iteration calls area(), but the actual computation depends on the object's class.
Why no instanceof?
Polymorphism eliminates the need for explicit type checks. As long as every subclass overrides area(), the loop handles any new subclass added later without modification.
Multi-subclass vocabulary.
The terms of polymorphic processing.
- Sibling subclasses — two classes that share the same parent.
- Polymorphic collection — array or list of the parent type holding mixed subclass objects.
- Uniform interface — the parent's method signatures, shared by all children.
- Open for extension — adding a new subclass doesn't break existing code.
- Dynamic dispatch — runtime method selection per object.
How multi-subclass polymorphism is tested.
Loop dispatch and reference vs object dominate.
- Parent-typed array, mixed objects. Each call dispatches per object.
- Same method name across subclasses. Enables uniform processing.
- Total / max / count across types. Common FRQ patterns.
- No instanceof needed. Polymorphism replaces type checks.
Total area of mixed shapes.
One loop; correct dispatch.
Input: { new Circle(2), new Square(3), new Circle(1) }.
Trace.
- Circle(2).area() = π * 4 ≈ 12.566.
- Square(3).area() = 9.
- Circle(1).area() = π ≈ 3.142.
Total: ≈ 24.708.
Why this matters. If you added a Triangle class with its own area override, the same loop would handle it — no code change required.
Multi-subclass pitfalls.
Most write code that defeats polymorphism.
- Using
instanceofto pick which area to compute. Defeats polymorphism. - Casting unnecessarily.
((Circle) sh).area()is needless when Shape declares area. - Forgetting to override in a subclass. That subclass uses parent's placeholder.
- Holding objects as subclass type when parent suffices. Limits reuse.
Polymorphic processor.
Three requirements.
Multi-Subclass Reference
AP Quick Reference| Requirement | Detail | AP Tip |
|---|---|---|
| Common parent | Both subclasses extend it | Defines the method. |
| Override in each | Subclass-specific logic | Signature matches. |
| Parent-typed array | Holds mixed objects | Polymorphic processing. |
| Single loop | Calls the shared method | Dispatch handles the rest. |
How to master multi-subclass polymorphism.
Trust dispatch; avoid type checks.
- MCQ Practice — for each loop, trace the dispatch per object before computing the total.
- Java Lab — build Shape/Circle/Square/Triangle. Write totalArea and verify it handles all three.
- FRQ Practice — write a polymorphic loop without
instanceof. Add a third subclass after to confirm no changes are needed.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 08 Topic 09 Two Subclasses and Polymorphism Two Subclasses and Polymorphism FRQ Practice
AP-style topic practice assessment