TypeScript Abstract Classes and Methods

Introduction

In this chapter, we will explore abstract classes and methods in TypeScript. Abstract classes and methods provide a way to define methods that must be implemented by derived classes. Understanding how to use abstract classes and methods is essential for creating a clear and structured class hierarchy in TypeScript programs.

Table of Contents

  • Definition
  • Abstract Class Syntax
  • Abstract Methods
  • Implementing Abstract Methods
  • Using Abstract Classes
  • Complete Example with Output
  • Conclusion

Definition

An abstract class is a class that cannot be instantiated directly and is meant to be subclassed. It can contain abstract methods, which are methods declared without an implementation. These abstract methods must be implemented by subclasses. Abstract classes can also contain regular methods with implementations.

Abstract Class Syntax

Syntax

abstract class ClassName {
  abstract methodName(parameters): returnType;

  // regular methods
  methodName2(parameters): returnType {
    // implementation
  }
}

Example

This example demonstrates a simple abstract class with an abstract method.

abstract class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  abstract makeSound(): void;

  move(): void {
    console.log(`${this.name} is moving.`);
  }
}

Abstract Methods

Abstract methods are methods declared in an abstract class without an implementation. These methods must be implemented by any subclass of the abstract class.

Example

This example demonstrates an abstract method makeSound in the Animal class.

abstract class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  abstract makeSound(): void;
}

Implementing Abstract Methods

Subclasses of an abstract class must implement all abstract methods declared in the abstract class.

Example

This example demonstrates a subclass Dog that implements the abstract method makeSound.

abstract class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  abstract makeSound(): void;
}

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }

  makeSound(): void {
    console.log(`${this.name} barks.`);
  }
}

let dog = new Dog("Tommy");
dog.makeSound(); // Output: Tommy barks.

Output

Tommy barks.

Using Abstract Classes

Abstract classes can contain both abstract methods and regular methods with implementations. Subclasses must implement all abstract methods but can inherit and use regular methods.

Example

This example demonstrates an abstract class with both abstract and regular methods.

abstract class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  abstract makeSound(): void;

  move(): void {
    console.log(`${this.name} is moving.`);
  }
}

class Cat extends Animal {
  constructor(name: string) {
    super(name);
  }

  makeSound(): void {
    console.log(`${this.name} meows.`);
  }
}

let cat = new Cat("Kitty");
cat.makeSound(); // Output: Kitty meows.
cat.move(); // Output: Kitty is moving.

Output

Kitty meows.
Kitty is moving.

Complete Example with Output

In this section, we will combine the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.

TypeScript Code

You can test the following code in the TypeScript Playground:

// Abstract Class and Abstract Methods
abstract class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  abstract makeSound(): void;

  move(): void {
    console.log(`${this.name} is moving.`);
  }
}

// Implementing Abstract Methods in Subclasses
class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }

  makeSound(): void {
    console.log(`${this.name} barks.`);
  }
}

class Cat extends Animal {
  constructor(name: string) {
    super(name);
  }

  makeSound(): void {
    console.log(`${this.name} meows.`);
  }
}

let dog = new Dog("Tommy");
dog.makeSound(); // Output: Tommy barks.
dog.move(); // Output: Tommy is moving.

let cat = new Cat("Kitty");
cat.makeSound(); // Output: Kitty meows.
cat.move(); // Output: Kitty is moving.

Conclusion

In this chapter, we covered abstract classes and methods in TypeScript, including how to define and use abstract classes and methods, and how to implement abstract methods in subclasses. We provided a complete example with its output to illustrate how these concepts work in TypeScript. Understanding abstract classes and methods is essential for creating a clear and structured class hierarchy in TypeScript programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare