All academies

Simulator

Composition vs Inheritance

Is-A vs Has-A. Prefer composition when you need flexible collaboration instead of a rigid taxonomy.

Has-A relationship

Composition builds objects from collaborating parts. NestJS services compose repositories, gateways, and helpers via DI — classic has-a design.

composition.ts
class Engine {
  start() { return "vroom"; }
}
class Car {
  constructor(private engine: Engine) {}
  start() { return this.engine.start(); }
} // Car has-a Engine