Abstract Class (TypeScript)

An abstract class is a base class that can't be instantiated on its own and may contain abstract methods that subclasses must implement. It defines a shared template for related classes.

TypeScript

Marking a class `abstract` means you can only create instances of classes that extend it. Abstract methods declare a name and signature but no body, forcing each subclass to provide its own implementation.

Abstract classes sit between interfaces and regular classes: like an interface they declare required members, but unlike an interface they can also include shared concrete code.

Syntax

abstract class Name {
  abstract method(): type;
}

Examples

TypeScript

abstract class Shape {
  abstract area(): number;
}

class Square extends Shape {
  constructor(private side: number) { super(); }
  area(): number { return this.side * this.side; }
}

console.log(new Square(3).area());

Output:

9

Learn it in a lesson

See also

Back to the Dictionary