intermediate

Abstract classes and interfaces

Choose interfaces for pure structural contracts and abstract classes when shared implementation or protected template methods are part of the design.

Interfaces define structural contracts with no implementation—ideal for ports and multiple inheritance of shape. Abstract classes provide shared implementation and protected template methods—use when subclasses genuinely extend a family algorithm.

TypeScript allows both; prefer interfaces for public APIs and abstract classes when you need guarded shared code—not for every hierarchy.

On interviews: choose interface vs abstract class for a `NotificationChannel` with email and SMS variants.

Common pitfalls: abstract classes as dumping grounds, implementing interfaces plus extending classes awkwardly, and diamond-like confusion when mixing paradigms.

The trade-off is flexible structural typing (interfaces) versus reusable base code (abstract classes).

Checklist:

  • Interface for pure contract.
  • Abstract class for shared algorithm skeleton.
  • Avoid deep abstract hierarchies.