TypeScript Abstract Class and Method

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


In TypeScript, an abstract class is a class that cannot be instantiated directly — you cannot create objects from it. We use the abstract keyword to declare an abstract class. For example,

// Create an abstract class
abstract class Shape {
  // Class content here
}

You can't create an instance of Shape class directly:

// Error: Cannot create an instance of an abstract class
const a = new Shape();

Note: Abstract classes are meant to be extended by other classes that provide implementations for their abstract methods.

An abstract class can have both abstract methods and regular methods (normal functions with implementation). For example,

abstract class Shape {
  // Abstract method
  abstract calculateArea(): void;

  // Regular method
  getDimensions(): void {
    console.log("The dimension is ");
  }
}

To learn about regular methods, visit TypeScript function. Here, we will learn about abstract methods.


Abstract Method

An abstract method is a function declared inside an abstract class without a body. It ends with a semicolon (;) and must be implemented by any subclass.

We use the same abstract keyword to create abstract methods. For example,

abstract class Shape {
  abstract calculateArea(): void;
}

Here,

  • calculateArea() is an abstract method.
  • The body of calculateArea() is replaced by a semicolon ;.

If a class contains at least one abstract method, then the class should be declared abstract. Otherwise, it will generate an error. For example,

// Error
// Class should be abstract
class Shape {
  abstract method1(): void;
}

Note: A class that extends an abstract class is called a subclass (or child class). It inherits all the non-abstract code and must implement all abstract methods.


Example 1: Abstract Class and Method

// Abstract class
abstract class Shape {
  protected dimension: number;

  constructor(dimension: number) {
    this.dimension = dimension;
  }

  // Abstract method
  abstract calculateArea(): number;
}

// 'Square' is a subclass that inherits from 'Shape'
class Square extends Shape {
  calculateArea(): number {
    return this.dimension * this.dimension;
  }
}

const square = new Square(5);
console.log("Area of square:", square.calculateArea());

Output

Area of square: 25

In the above example, we have created an abstract class named Shape that includes an abstract method calculateArea().

The Square class extends Shape and provides a concrete implementation of the calculateArea() method.

We then created an instance of the Square class and called the calculateArea() method using that instance:

square.calculateArea();

This method returns the area of the square, which is then logged to the console using:

console.log("Area of square:", square.calculateArea());

Example 2: Abstract Class with Multiple Subclasses

// Abstract class
abstract class Animal {
  abstract Sound(): void;
}

// Sub class
class Dog extends Animal {
  Sound(): void {
    console.log("Woof! Woof!");
  }
}

// Sub class
class Cat extends Animal {
  Sound(): void {
    console.log("Meow!");
  }
}

const dog = new Dog();
dog.Sound();  

const cat = new Cat();
cat.Sound();  

Output

Woof! Woof!
Meow!

Here, both Dog and Cat extend the abstract class Animal and implement the Sound() method.

We create their objects and call the method to produce different outputs.


Why Use Abstract Classes?

We use abstract classes in TypeScript for the following reasons:

  • To define a common structure for related classes.
  • To make sure all child classes implement specific methods.
  • To write shared code for all child classes, while letting them define their own specific parts.

Read More:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges