Polymorphic Behaviors
Use polymorphism to print, sort, and aggregate mixed shapes. Keep algorithms tiny and easy to read for mobile.
Overview
Polymorphic behaviors let us treat all shapes the same while each runs its own math. We will: (1) print a mixed list uniformly, (2) sort by area, and (3) compute totals with one loop.
Base API checklist
Base API keeps the actions used everywhere:
- area() and perimeter() — must be implemented by each shape.
- Optional tiny helper like label() or info() for neat printing.
- Keep fields inside concrete shapes.
All lessons in this course
- Design
- Implementation
- Polymorphic Behaviors