What Are Abstract Classes in TypeScript?

Learn What Are Abstract Classes in TypeScript? with practical examples, explanations, best practices, and real-world coding techniques.

Introduction to Abstract Classes

Abstract classes in TypeScript provide a foundation for creating related classes that share common behavior but may implement certain operations differently. They define a common structure while leaving some implementation details to the classes that extend them.

Consider an application that supports different payment methods, such as card payments, bank transfers, and digital wallets. Every payment method may need to validate an amount and expose a processing operation, but the actual payment process differs depending on the method.

An abstract class can define the common properties and methods required by every payment method. It can also declare abstract methods that each concrete subclass must implement.

TypeScript uses the abstract keyword to declare abstract classes and abstract members. An abstract class cannot be instantiated directly, but other classes can extend it to create usable objects.

1. Understanding Abstract Class Syntax

An abstract class is declared using the abstract keyword before the class name. It can contain ordinary properties, implemented methods, constructors, and abstract members that subclasses must implement.

abstract class Animal {
  constructor(public name: string) {}

  abstract makeSound(): string;

  describe(): string {
    return this.name + " is an animal.";
  }
}

The Animal class contains an abstract method called makeSound and a concrete method called describe. The abstract method declares the required behavior without providing its implementation.

The describe method already has an implementation and can be inherited by subclasses. This allows the base class to provide shared functionality while requiring specific behavior from its descendants.

You cannot create an instance directly using new Animal("Buddy"). TypeScript reports an error because Animal is abstract. Instead, you must create a concrete class that extends Animal and implements its required abstract members.

2. Implementing an Abstract Class

A concrete class extends an abstract class using the extends keyword. It must implement all inherited abstract members that remain unimplemented.

abstract class Animal {
  constructor(public name: string) {}

  abstract makeSound(): string;

  describe(): string {
    return this.name + " is an animal.";
  }
}

class Dog extends Animal {
  override makeSound(): string {
    return "Woof!";
  }
}

class Cat extends Animal {
  override makeSound(): string {
    return "Meow!";
  }
}

const dog = new Dog("Buddy");
const cat = new Cat("Milo");

console.log(dog.makeSound());
console.log(cat.makeSound());

console.log(dog.describe());

Dog and Cat both extend Animal and provide their own implementations of makeSound. Each subclass also inherits the constructor and describe method.

The base class establishes the shared contract, while the subclasses supply their specific behavior. This keeps the shared implementation in one place and avoids duplicating the describe method.

The override keyword indicates that a method intentionally replaces an inherited method or satisfies an inherited abstract method. It also helps the compiler detect certain mistakes, such as attempting to override a nonexistent member.

3. Understanding Abstract Methods

An abstract method defines a method signature without providing a method body. It describes what a concrete subclass must implement, including the method name, parameters, and return type.

abstract class PaymentMethod {
  abstract processPayment(amount: number): boolean;

  validateAmount(amount: number): boolean {
    return Number.isFinite(amount) && amount > 0;
  }
}

class CardPayment extends PaymentMethod {
  override processPayment(amount: number): boolean {
    if (!this.validateAmount(amount)) {
      return false;
    }

    console.log("Processing card payment:", amount);
    return true;
  }
}

const payment = new CardPayment();

console.log(payment.processPayment(500));

The processPayment method is abstract, so every concrete subclass must provide a compatible implementation. The validateAmount method is concrete and supplies reusable behavior directly from the base class.

CardPayment uses the inherited validation method before continuing with its own processing logic. Other subclasses can reuse the same validation method while implementing their own processing behavior.

Abstract methods cannot contain implementations in the abstract class. If a method already has a valid shared implementation, declare it as an ordinary method rather than making it abstract unnecessarily.

4. Understanding Abstract Properties

Abstract classes can also declare abstract properties. These properties define information that concrete subclasses must provide.

abstract class Shape {
  abstract color: string;

  abstract getArea(): number;

  describe(): string {
    return "This shape is " + this.color;
  }
}

class Rectangle extends Shape {
  constructor(
    public width: number,
    public height: number,
    public color: string
  ) {
    super();
  }

  override getArea(): number {
    return this.width * this.height;
  }
}

const rectangle = new Rectangle(10, 5, "blue");

console.log(rectangle.getArea());
console.log(rectangle.describe());

The Shape class declares an abstract color property and an abstract getArea method. Rectangle satisfies both requirements by providing its own color property and area calculation.

The constructor calls super() because Rectangle extends a base class. It also initializes the dimensions and color through constructor parameter properties.

Abstract properties are useful when different subclasses need to expose the same information but determine its actual value in their own implementations.

5. Constructors in Abstract Classes

An abstract class can have a constructor just like an ordinary class. The constructor initializes the shared state that every concrete subclass inherits.

abstract class Employee {
  constructor(
    public name: string,
    public department: string
  ) {}

  getDetails(): string {
    return this.name + " works in " + this.department;
  }

  abstract getRole(): string;
}

class Developer extends Employee {
  constructor(
    name: string,
    department: string,
    public language: string
  ) {
    super(name, department);
  }

  override getRole(): string {
    return "Developer";
  }
}

const developer = new Developer(
  "Abhinash",
  "Engineering",
  "TypeScript"
);

console.log(developer.getDetails());
console.log(developer.getRole());

The Employee constructor initializes the shared name and department properties. Developer passes those values to the base constructor through super(name, department) and initializes its additional language property.

An abstract class can initialize common fields, validate initial values, and provide shared setup logic. These responsibilities should remain focused on the common requirements of its subclasses.

Even though Employee cannot be instantiated directly, its constructor is invoked when a concrete subclass is created. The abstract modifier restricts direct instantiation; it does not prevent constructor execution as part of inheritance.

6. Understanding Concrete and Abstract Methods Together

One of the main benefits of abstract classes is that they can combine implemented methods with methods that subclasses must provide. This lets the base class control shared workflows while delegating specific steps.

abstract class ReportGenerator {
  generate(): string {
    const content = this.getContent();
    return this.addHeader(content);
  }

  protected abstract getContent(): string;

  private addHeader(content: string): string {
    return "REPORT\n" + content;
  }
}

class SalesReport extends ReportGenerator {
  protected override getContent(): string {
    return "Sales increased by 12%.";
  }
}

class UserReport extends ReportGenerator {
  protected override getContent(): string {
    return "There are 500 active users.";
  }
}

const sales = new SalesReport();
const users = new UserReport();

console.log(sales.generate());
console.log(users.generate());

The generate method implements the shared workflow: obtain the content and add a header. The private addHeader method keeps an internal implementation detail inside the base class.

Each concrete subclass supplies the content through getContent. The caller can invoke generate without needing to know how each report's content is produced.

This design resembles the template method pattern. It is useful when multiple classes follow the same overall process but need different implementations for one or more steps.

7. Using Access Modifiers in Abstract Classes

Abstract classes support public, protected, and private members, just like ordinary classes. These modifiers determine how shared behavior and implementation details are exposed.

abstract class DataProcessor {
  constructor(protected source: string) {}

  public process(): string {
    const data = this.readData();
    return this.formatData(data);
  }

  protected abstract readData(): string;

  private formatData(data: string): string {
    return "Processed: " + data;
  }
}

class FileProcessor extends DataProcessor {
  protected override readData(): string {
    return "File data from " + this.source;
  }
}

const processor = new FileProcessor("report.txt");

console.log(processor.process());

// Compilation error:
// processor.readData();

// Compilation error:
// processor.source;

The public process method is available to external callers. The protected source property and readData method are accessible inside the class hierarchy, but not through ordinary external access.

The private formatData method remains an internal implementation detail of DataProcessor. It is used by the base class's public workflow and is not directly available to subclasses.

Choosing clear visibility rules makes abstract classes easier to extend without exposing more internal details than subclasses actually need.

8. Abstract Classes and Polymorphism

Abstract classes support polymorphism by allowing several concrete subclasses to be referenced through the same abstract base type. The base class defines the expected interface, while each subclass provides its own implementation.

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

class Rectangle extends Shape {
  constructor(
    public width: number,
    public height: number
  ) {
    super();
  }

  override getArea(): number {
    return this.width * this.height;
  }
}

class Circle extends Shape {
  constructor(public radius: number) {
    super();
  }

  override getArea(): number {
    return Math.PI * this.radius * this.radius;
  }
}

function printArea(shape: Shape): void {
  console.log(shape.getArea());
}

printArea(new Rectangle(10, 5));
printArea(new Circle(3));

The printArea function accepts any concrete object that extends Shape and implements getArea. It does not need separate branches for Rectangle and Circle.

When the function calls getArea, the implementation belonging to the actual instance executes. Rectangle calculates its area using width and height, while Circle uses its radius.

Polymorphism allows new shapes to be introduced without rewriting the shared function, as long as each shape satisfies the abstract contract.

9. Abstract Classes vs. Interfaces

Abstract classes and interfaces can both describe requirements that other classes must satisfy, but they serve different purposes.

Feature Abstract class Interface
Implemented methods Can contain shared implementations Describes member contracts rather than providing runtime method implementations
Constructor Can initialize inherited instance state Does not have a class constructor
State and implementation Can contain properties and implemented behavior Primarily defines a structural contract
Usage Extended using extends Implemented by classes using implements
Multiple contracts A class can extend only one class directly A class can implement multiple interfaces
Direct instantiation Cannot be instantiated when abstract Cannot be instantiated because an interface is a type declaration

Use an abstract class when related classes should share state, constructors, or implemented methods alongside mandatory operations. Use an interface when you mainly need to describe a contract that different classes or objects can satisfy.

An abstract class can also implement one or more interfaces, combining shared implementation with additional structural contracts when that design is useful.

10. Common Mistakes to Avoid

  • Instantiating an abstract class: create a concrete subclass and instantiate that class instead.
  • Forgetting to implement abstract members: a concrete subclass must implement all inherited abstract members that remain unimplemented.
  • Adding a method body to an abstract method: abstract methods declare the required signature but do not provide an implementation in the abstract class.
  • Forgetting super() in a derived constructor: a concrete subclass with its own constructor must call super before accessing this.
  • Marking every method abstract: provide concrete methods when the base class can offer behavior shared by its subclasses.
  • Confusing interfaces with abstract classes: interfaces define contracts, while abstract classes can also own instance state and shared implementation.
  • Creating overly complicated base classes: keep shared behavior focused and avoid combining unrelated responsibilities in a single hierarchy.
  • Assuming abstract members create runtime enforcement: TypeScript's abstract restrictions are primarily compile-time checks and do not replace runtime validation.

11. Best Practices for Abstract Classes

  • Use abstract classes when related implementations share a meaningful base structure or common workflow.
  • Declare only essential subclass requirements as abstract members.
  • Provide concrete methods for logic that every subclass can reuse safely.
  • Keep base constructors focused on initializing common state and enforcing shared initialization rules.
  • Choose access modifiers carefully so subclasses receive only the access they need.
  • Use the override keyword for inherited method implementations when supported by the project's TypeScript version.
  • Accept abstract base types in reusable functions when the implementation should work with several concrete subclasses.
  • Prefer interfaces when multiple unrelated classes need to satisfy a common contract without sharing an implementation.
  • Use composition when behavior can be shared more cleanly through separate objects rather than an inheritance hierarchy.
  • Keep runtime validation separate from compile-time abstract class constraints when handling untrusted data.

Conclusion

Abstract classes in TypeScript define shared structures for related classes while requiring concrete subclasses to implement specific behavior. They can contain constructors, initialized properties, concrete methods, abstract methods, and abstract properties.

By combining shared implementations with mandatory subclass contracts, abstract classes reduce duplication and support polymorphism. They are especially useful when multiple classes follow a common workflow but differ in specific operations.

Choose abstract classes when shared state and implementation are important, and use interfaces when a structural contract is sufficient. Keeping these abstractions focused helps create TypeScript applications that are reusable, consistent, and easier to maintain.

Code Examples

Create an Abstract Class

typescript
abstract class Animal {
  constructor(public name: string) {}

  abstract makeSound(): string;

  describe(): string {
    return this.name + " is an animal.";
  }
}

class Dog extends Animal {
  override makeSound(): string {
    return "Woof!";
  }
}

const dog = new Dog("Buddy");

console.log(dog.makeSound());
console.log(dog.describe());

// Compilation error:
// const animal = new Animal("Unknown");
    

Implement Abstract Properties

typescript
abstract class Shape {
  abstract color: string;
  abstract getArea(): number;
}

class Rectangle extends Shape {
  constructor(
    public width: number,
    public height: number,
    public color: string
  ) {
    super();
  }

  override getArea(): number {
    return this.width * this.height;
  }
}

const rectangle = new Rectangle(10, 5, "blue");

console.log(rectangle.color);
console.log(rectangle.getArea());
    

Combine Concrete and Abstract Methods

typescript
abstract class PaymentMethod {
  abstract processPayment(amount: number): boolean;

  validateAmount(amount: number): boolean {
    return Number.isFinite(amount) && amount > 0;
  }
}

class CardPayment extends PaymentMethod {
  override processPayment(amount: number): boolean {
    if (!this.validateAmount(amount)) {
      return false;
    }

    console.log("Processing payment:", amount);
    return true;
  }
}

const payment = new CardPayment();

console.log(payment.processPayment(500));
    

Use Abstract Classes with Polymorphism

typescript
abstract class Shape {
  abstract getArea(): number;
}

class Rectangle extends Shape {
  constructor(
    public width: number,
    public height: number
  ) {
    super();
  }

  override getArea(): number {
    return this.width * this.height;
  }
}

class Circle extends Shape {
  constructor(public radius: number) {
    super();
  }

  override getArea(): number {
    return Math.PI * this.radius * this.radius;
  }
}

function printArea(shape: Shape): void {
  console.log(shape.getArea());
}

printArea(new Rectangle(10, 5));
printArea(new Circle(3));
    

Implement a Shared Report Workflow

typescript
abstract class ReportGenerator {
  generate(): string {
    return "REPORT\n" + this.getContent();
  }

  protected abstract getContent(): string;
}

class SalesReport extends ReportGenerator {
  protected override getContent(): string {
    return "Sales increased by 12%.";
  }
}

class UserReport extends ReportGenerator {
  protected override getContent(): string {
    return "There are 500 active users.";
  }
}

console.log(new SalesReport().generate());
console.log(new UserReport().generate());
    

Initialize an Abstract Base Class

typescript
abstract class Employee {
  constructor(
    public name: string,
    public department: string
  ) {}

  getDetails(): string {
    return this.name + " works in " + this.department;
  }

  abstract getRole(): string;
}

class Developer extends Employee {
  constructor(
    name: string,
    department: string,
    public language: string
  ) {
    super(name, department);
  }

  override getRole(): string {
    return "Developer";
  }
}

const developer = new Developer(
  "Abhinash",
  "Engineering",
  "TypeScript"
);

console.log(developer.getDetails());
console.log(developer.getRole());
    

Pro Tip

Use an abstract class when subclasses should share state or reusable behavior while implementing a few required operations themselves. Keep the common workflow in concrete base methods and leave only the variable behavior abstract. If you need to define a contract without sharing implementation or state, consider using an interface instead.

More TypeScript lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.