LOAD-BEARINGfree
Low-level designTier 2 · unit 4 of 5 · 11 min read

Composition over inheritance

Inheritance is usually the first "reuse" tool anyone learns, which is unfortunate, because it's also the tightest coupling that exists in object-oriented programming. Let's see why, watch it fail in slow motion, and learn the alternative that scales.

Why inheritance couples so hard

When B extends A, B doesn't just depend on A's public interface — it depends on A's implementation. B's methods run against A's internal state; B may override some methods and inherit others that call each other in ways B can't see. Change something private inside A, and B — which never mentioned that code — breaks. This has a name, the fragile base class problem, and every large inheritance hierarchy eventually stars in it.

The failure, in slow motion

You're modelling vehicles. Day one: Vehicle → Car, and life is good. Then: electric cars. Fine — Car → ElectricCar. Then someone asks for an electric truck. Hmm — ElectricTruck extends... Truck? But the battery logic lives in ElectricCar. Copy it? Then hybrids arrive. Then autonomous vehicles. Now you need AutonomousElectricTruck, and every combination of every axis wants its own class:

// inheritance: ONE hierarchy, but reality has THREE axes
class ElectricAutonomousTruck extends ...???   // extends what, exactly?

// composition: each axis varies independently
class Vehicle {
  Engine engine;        // Petrol | Electric | Hybrid
  Controller control;   // Human  | Autonomous
  Chassis chassis;      // Car    | Truck | Bike
}

See the arithmetic: with inheritance, 3 engines × 2 controllers × 3 chassis = 18 classes, most containing duplicated logic. With composition, it's 3 + 2 + 3 = 8 small classes, and "autonomous electric truck" is just three field values. Add a fourth engine type and inheritance demands six new classes; composition demands one.

The root cause is structural, not a skill issue: inheritance can express exactly one hierarchy, and reality usually has several independent axes of variation. Composition gives each axis its own small hierarchy and mixes them freely.

When inheritance IS the right tool

Not never! Inheritance earns its place when all three hold: there's a genuine is-a relationship (not just "shares some code"), the hierarchy is shallow (an abstract base with one level of concrete children is usually fine; three levels deep is a smell), and subtypes never need to weaken or surprise the parent's behaviour (remember Liskov from 2.02).

And the anti-reason, stated plainly: "I want to reuse that code" is not a valid reason to inherit. Reuse is what composition and delegation are for — hold a reference to the thing, call its methods. Inheriting for reuse buys you the coupling without the meaning.

The interview signal

When a design question has variation in it — and they all do — interviewers are specifically watching your first instinct: do you reach for a class hierarchy, or do you model the varying dimensions as separately composed collaborators? "The vehicle has an engine" beats "the vehicle is a subtype per engine" almost every time. Practise until has-a is your reflex and is-a needs justification.