MEPX
Chapter 3 of 10All chapters

Chapter 3 of 10

Inheritance

Reusing a definition, with a catch.

How it works

A subclass gets everything the parent has and may add or replace parts. It expresses an is-a relationship: a Manager is an Employee.

  • A subclass must be usable anywhere the parent is, or callers break.
  • A square subclassing a rectangle is the classic example of that rule being violated.

The cost

Inheritance is the tightest coupling available: a change to the parent reaches every descendant. Deep hierarchies become hard to reason about and harder to change.