Skip to content

Abstract Classes

An abstract class is a class that cannot be instantiated directly. It’s designed to be a base class that other classes extend. It can contain both abstract methods (no implementation) and concrete methods (with implementation).


An abstract “Vehicle” blueprint:

  • It defines shared parts every vehicle has (wheels, engine, steering) — concrete methods
  • It says every vehicle must move(), but doesn’t specify how — abstract methods
  • You can’t build a “Vehicle” directly (it’s abstract)
  • You can build a “Car”, “Bike”, or “Truck” from it

abstract class Report {
constructor(protected title: string) {}
// Abstract methods — subclasses must implement
abstract generate(): string;
abstract getFormat(): string;
// Concrete method — shared behavior
print(): void {
console.log(`Printing report: ${this.title}`);
}
}
class PDFReport extends Report {
generate(): string { return "PDF content"; }
getFormat(): string { return "PDF"; }
}

Use Abstract Class when:

  • Classes share common state (fields)
  • Classes share common code (concrete methods)
  • Classes are closely related in hierarchy

Use Interface when:

  • You want to define a capability across unrelated classes
  • You need multiple inheritance of behavior
  • You want a pure contract without any implementation

Previous Topic: Interfaces → Next Topic: Object Lifecycle →