TypeScript theorytheory 0/50 · 0%
Foundations · medium

31. Abstract classes

Base classes that can't be instantiated directly.

An `abstract class` may declare `abstract` methods with no implementation, forcing subclasses to provide one. It cannot be instantiated with `new` directly.

ts
abstract class Token {
  constructor(public symbol: string) {}
  abstract transfer(to: string, amount: number): boolean;
  describe(): string {
    return `Token: ${this.symbol}`;
  }
}

class Erc20 extends Token {
  transfer(to: string, amount: number): boolean {
    console.log(`sending ${amount} to ${to}`);
    return true;
  }
}

// new Token("X"); // error: cannot instantiate an abstract class
const t = new Erc20("USDC");

Abstract classes combine shared implementation (`describe`) with an enforced contract (`transfer`) — a middle ground between a plain interface (no shared code) and a concrete base class (nothing enforced).

Check your understanding

  1. 1. Can you do `new Token(...)` if `Token` is an abstract class?

  2. 2. What must a concrete subclass of an abstract class do?

  3. 3. What is the benefit of an abstract class over a plain interface?