Polymorphism
LockedLesson 14 of 60 · OOP · Medium
Finish these first:Inheritance →Or pass the tests below to unlock it now.
The big idea
A reference of a supertype can hold subtype objects; method calls dispatch dynamically to the runtime type. Use instanceof pattern matching sparingly; prefer polymorphic methods.
Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Read the lesson
A reference of a supertype can hold subtype objects; method calls dispatch dynamically to the runtime type.
List<Shape> shapes = List.of(new Circle(1), new Rect(2, 3));
for (Shape s : shapes) total += s.area(); // dynamic dispatch
Use instanceof pattern matching sparingly; prefer polymorphic methods.
Quick check
Before writing code: What should `Solution.totalArea(new Solution.Shape[]{() -> 2, () -> 3})` return when your implementation of Polymorphism is working?
Your challenge
Write static int totalArea(Shape[] shapes) using polymorphic calls.
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
See how Java compares side-by-side with Python.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Polymorphism.
Learn this first
- InheritanceNot yet
Opens up next
This is the last lesson in this branch.