Advertisement
Advanced TypeScript, Classes: Access Modifiers, Abstract Classes & Implements

What is the difference between an abstract class and an interface when both could technically express the same contract, and how do you decide which to use?

Short answer

An abstract class can provide shared implementation, state, and constructor logic that subclasses inherit for free, and supports access modifiers — but a class can only extend one. An interface is purely a shape contract with zero implementation, and a class can implement many.

Save TypeScript mock interview
Advertisement
AModel answer
An abstract class can provide shared implementation, state, and constructor logic that subclasses inherit for free, and supports access modifiers — but a class can only extend one. An interface is purely a shape contract with zero implementation, and a class can implement many. Choose an abstract class when subclasses genuinely share behavior/state; choose an interface when you only need to enforce a shape across otherwise unrelated classes.
// Shared behavior -> abstract class
abstract class Repository<T> {
  protected items: T[] = [];
  abstract validate(item: T): boolean;
  add(item: T) { if (this.validate(item)) this.items.push(item); }
}

// Pure contract -> interface
interface Identifiable { id: string; }
Real-world example
Choosing an abstract Repository base class with shared CRUD logic, versus a plain Identifiable interface used across many unrelated types.
Likely follow-ups
  • Can TypeScript interfaces declare and enforce a constructor signature the way abstract classes can?
Related topics