Skip to content

OOP and Composition over Inheritance ​

Explain the four pillars by what they cost, say why a hierarchy stops working at the fourth level, and show the shapes that replace it.

In this chapter: state plus the rules about it · private vs # · structural polymorphism · what deep hierarchies cost · has-a, dependency injection and higher-order functions

💡 The Core Idea ​

Object-oriented programming answers one question: where does state live, and who may change it? An object answers "here, and only through these methods". Encapsulation, inheritance, polymorphism and abstraction all follow from that answer.

Inheritance is the pillar that goes wrong. It couples a subclass to everything its ancestors do, including the parts it never wanted. Composition couples an object only to the interfaces it asks for. "Prefer composition" is not about taste. It is about which changes stay safe.

How It Works ​

Objects are state plus the rules about it ​

typescript
class Report {
  private readonly audit: { at: Date; action: string }[] = [];

  // Parameter properties declare and assign a field in one place
  constructor(
    public readonly id: string,
    private title: string,
  ) {}

  rename(next: string, by: string): void {
    if (next.trim().length === 0) throw new Error('Title cannot be empty');
    this.title = next;
    this.audit.push({ at: new Date(), action: `renamed by ${by}` });
  }
}

The rule "a title is never empty" sits beside the field it protects, so no caller can break it. The audit trail cannot be forgotten, because the only way to change the title also records the change.

What private actually means ​

ModifierEnforced byVisible to
public (default)—Everyone
protectedThe compilerThe class and its subclasses
privateThe compiler onlyThe class
#fieldThe JavaScript runtimeThe class, at runtime too

That third row is the interview question. (s as any).token reaches a private field at runtime. s.#secret is a SyntaxError that no cast defeats. Use private for ordinary design boundaries. Use # when a value must not leak even to misbehaving code, such as a token a third-party script could reach.

⚠️ A getter and setter for every field is not encapsulation. If every private field has a public pair, the object is a bag of data with more code. A setter earns its place when it validates, normalises or records.

Polymorphism removes conditionals ​

The caller depends on a shape, not a concrete type. So a new implementation does not edit the caller. TypeScript is structural: any object with the right members satisfies an interface, with or without implements.

FormMechanismResolved
SubtypeAn interface with several implementationsAt runtime
ParametricGenerics — Repository<T> works for any TAt compile time
StructuralAny object with the right shape fits the typeAt compile time

Prefer an interface for abstraction. Reach for an abstract class only when implementations share real behaviour. The usual case is a fixed sequence of steps with one step left open: the template method.

What a deep hierarchy costs ​

typescript
// ❌ Four levels, and the fourth has to break the contract
class Employee { work(): void {} }
class Manager extends Employee { approve(): void {} }
class RegionalManager extends Manager { setBudget(): void {} }

class ContractRegionalManager extends RegionalManager {
  approve(): never {
    throw new Error('Contractors cannot approve'); // any code holding a Manager can now throw
  }
}

That override breaks the Liskov substitution principle, which says a subtype must work anywhere its parent does. Code holding a Manager can now fail for reasons it cannot see.

CostWhat it looks like
Fragile base classA change in Employee breaks classes its author has never opened
Behaviour is hard to find"Where does work actually run?" needs four files
One axis onlyA class has one parent, so role × contract × region needs a class per mix

Has-a instead of is-a ​

Composition models each axis as its own value and gives the object the ones it needs.

typescript
interface ApprovalRights {
  canApprove(amount: number): boolean;
}

class Staff {
  constructor(
    public readonly name: string,
    private readonly approval: ApprovalRights,
    private readonly budget?: { limit: number },
  ) {}

  approve(amount: number): boolean {
    return this.approval.canApprove(amount) && amount <= (this.budget?.limit ?? 0);
  }
}

const contractor = new Staff('Priya', { canApprove: () => false }); // any mix, no class per mix

The behaviour is now a value. You can swap it at runtime, test it alone, and reuse it. A superclass offers none of those three.

Dependency injection is composition with a rule ​

A class declares the collaborators it needs and is handed them, instead of building them.

typescript
class ReportService {
  // Depends on two interfaces — not on Postgres, and not on the system clock
  constructor(
    private readonly store: ReportStore,
    private readonly clock: Clock,
  ) {}

  async isStale(id: string): Promise<boolean> {
    const report = await this.store.findById(id);
    if (report === null) return false;
    return this.clock.now().getTime() - report.updatedAt.getTime() > 86_400_000;
  }
}

Testing is the whole argument. Suppose ReportService built its own database client and called new Date(). Then testing "is a report stale after a day?" would need a database and a time machine. With both injected, the test is two object literals. It needs no class and no mocking library.

A cross-cutting concern is one that many handlers share, such as logging or auth. For stateless ones, a higher-order function is composition too. withAuth(withLogging(getReport)) wraps a handler twice. That is the decorator pattern with no classes.

⚠️ Injecting everything is its own smell. A constructor with nine dependencies is telling you the class does nine things. Fix the class, not the wiring.

When to Use It ​

SituationChooseWhy
Genuine "is a", stable, one or two levelsInheritanceThe shared code is real and unlikely to fork
A fixed algorithm with one varying stepInheritance (template method)The sequence itself is the shared thing
Behaviour varies on more than one axisCompositionSingle inheritance cannot express two axes
Swappable behaviour, or isolated testsComposition and injectionSwap a field; pass a collaborator in
A subclass would override a method to do nothingCompositionThat override is a Liskov violation waiting to be found

Framework base classes and error hierarchies are reasonable inheritance. An example is class NotFoundError extends HttpError. The rule: inheritance answers "these things share behaviour", never "these things share some fields".

Common Mistakes ​

❌ Anaemic objects. Public fields plus a separate service that changes them is procedural code with class syntax. ✅ Put the rule next to the state it protects, so an invalid object cannot be built.

❌ Inheriting to reuse a helper method. The subclass now carries the parent's whole surface. ✅ Move the helper into a module or an injected collaborator.

❌ An interface for everything, straight away. One implementation behind an interface is indirection. ✅ Add the interface at the second implementation, or when it is what makes the test possible.

❌ A service locator instead of injection. A class that calls container.get('db') hides its dependencies again. ✅ Take the dependency as a constructor parameter, so the signature documents it.

🔑 Key Takeaways ​

  • An object puts state and the rules about that state in one place. Every pillar follows from that.
  • TypeScript's private is a compile-time check that a cast defeats. The runtime enforces #field.
  • TypeScript is structural, so any object with the right shape satisfies an interface. That makes test doubles cheap.
  • A hierarchy breaks when behaviour varies on more than one axis, because a class has exactly one parent.
  • Dependency injection is composition applied to collaborators, and its payoff is a unit you can test without infrastructure.

Interview Questions ​

Q: What is the difference between private and # in a TypeScript class?

The compiler enforces private and then erases it from the JavaScript. So a cast to any reaches the field at runtime. # is a JavaScript private field that the runtime enforces. Reading it from outside is a syntax error. Use private for design boundaries and # when the value must not leak.

Q: "Favour composition over inheritance" — why?

Inheritance couples you to everything the ancestors do, when usually you wanted one piece of behaviour. Composition names that piece and makes it swappable. It also keeps each part testable alone. Inheritance still wins for a genuine "is a" whose shared code is a stable algorithm.

Q: Show me a case where inheritance is the right call.

A template method: a fixed sequence where one step varies. For example, validate, then deliver, where only delivery differs per channel. The shared thing is the sequence. An interface cannot express it, and composition would repeat it in every implementation. Error hierarchies are the other honest case.

Q: TypeScript is structurally typed. What does that change?

Substitutability depends on shape, not declaration. Test doubles and adapters become cheap, because an object literal can satisfy a dependency. But two unrelated types with the same shape are also interchangeable, which can hide a real bug. A branded type (a type with a unique tag field added) is the escape hatch when you need nominal behaviour, where types match by name.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.