Low-Level Design7h estimated

Inheritance & Polymorphism

Difficulty
Importance

The problem

Many types share common behavior and differ only in specific details — writing every method fresh for each one duplicates the shared parts, and treating each type as unrelated to the others makes it impossible to write code that works generically across all of them.

Why now

Object-oriented programming basics established the class as a unit of data and behavior; inheritance and polymorphism are how classes relate to each other — and JavaScript's prototype chain already showed one concrete implementation of 'delegate to a shared definition,' which is the exact mechanism classical inheritance formalizes with an explicit class hierarchy.

Mental model

Inheritance lets a subclass reuse a superclass's behavior and override only what differs — the same delegate-up-the-chain idea prototypes-and-inheritance already covered, just with an explicit, named hierarchy instead of an implicit link. Polymorphism is the payoff: code written against the general type (Shape) works correctly on any specific subtype (Circle, Square) without knowing which one it's holding, because each overrides area() with its own correct behavior.

Requires

Unlocks

Used in

Projects

  • Design a Shape base class with Circle, Rectangle, and Triangle subclasses, each overriding an area() method, and write one function that computes total area over a mixed list of shapes without any type-checking
  • Deliberately misuse inheritance (subclass something it shouldn't be, e.g. Square extends Rectangle) and identify the resulting bug, then discuss composition-over-inheritance as a way to see the fix

Examples

  • A List<Shape> can hold Circles and Squares interchangeably; calling shape.area() on each runs the correct, type-specific implementation automatically
  • A Square that inherits from Rectangle and overrides setWidth to also change height silently breaks any code that assumed a Rectangle's width and height could vary independently — a classic inheritance misuse

Resources

Mastery checklist