Typescript abstract class | Beginner tutorial & examples

Typescript Abstract class: Learn with examples.

In this Blog Post, Learn the Abstract class tutorials in typescript.

It is an important concept of Object Oriented Programming. Below are Other concepts

You can also check Typescript final keyword

What is Abstract Class in Typescript

Typescript supports object-oriented programming concepts using classes, interfaces, and abstract classes.

Class defined with the abstract keyword is called abstract classes. It is used to provide an abstraction.

Instances for abstract classes are not created. However, instances created by the classes extend abstract classes.

Abstract methods contain a signature, not method implementations.

Here is an Abstract class Syntax

abstract class abstractclass{
}

The addition of the abstract keyword indicates that it is an abstract class, which typically contains only abstract methods.

What are Abstract Methods in TypeScript?

Methods without implementation or body are called abstract methods. They solely consist of method signatures without providing any implementation.

Abstract methods are declared by appending the abstract keyword to them and can be implemented by classes that extend the abstract class. However, the class itself must also be declared as abstract if it contains any abstract methods.

Abstract classes encompass both abstract methods and non-abstract methods.

Here is an abstract method syntax

abstract method(): returntype;

Let’s explore examples of abstract classes containing both abstract and non-abstract methods:

Following is an abstract, containing the abstract method -getRole() and a nonabstract method - getName().

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

  getName(): void {
    console.log("name: " + this.name);
  }

  abstract getRole(): void; //Abstract method must be implemented in subclasses
}

Let’s see what happens if we create an instance of an abstract class. The below code creates an instance of an abstract class. And It gives compilation error:Can not create an instance of an abstract class.

let employee: Employee; // It is valid as an instance reference creation
employee = new Employee("Kiran", "Developer"); // compilation error:Can not create an instance of an  abstract class

How to Extend Abstract class Inheritance Example

Subclasses can extend abstract classes just like normal classes and are required to provide implementations for abstract methods.

In the following example, Created an instance of a subclass, and provided implementation for abstract class methods.

class HrEmployee extends Employee {
  constructor(public name: string, public role: string) {
    super(name, role); // Constructor has to call super()
  }

  getHRRoles(): void {
    console.log(name + "has an " + this.role);
  }

  getRole(): void {
    console.log("HR Manager");
  }
}

let employee: Employee; // It is valid as instance reference creation
employee = new HrEmployee("kiran", "hrmanager");
employee.getHRRoles(); // Compilation error as getHRRoles() method does not exist on abstract class
employee.getName();

How to define a constructor in Abstract classes?

Creating an object of an abstract class results in an error, as object creation invokes the constructor which abstract classes lack. However, constructors can be defined within abstract classes, ensuring that subclasses calling these constructors will initialize properly.

employee = new Employee("Kiran", "Developer"); // compilation error:Can not create an instance of an abstract class

You can create a constructor in the Abstract class. The subclasses which extend the abstract class should call the constructor while creating an instance of subclasses.

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

How to define abstract setter and getter in class?

Abstract classes can declare abstract member variables and accessor methods like setter and getter using TypeScript’s set and get accessor types.

Here is an example

abstract class Parent {
  abstract name: string;
  abstract get value();
  abstract set value(v: number);
}

Typescript Abstract class implements the interface

Abstract classes can implement interfaces but must provide implementations for abstract methods; otherwise, it will result in a compilation error Class ‘MyClass’ incorrectly implements interface‘MyInterface’.

interface MyInterface {
  myMethod();
}
abstract class MyClass implements MyInterface {
  abstract myMethod();
}

How to declare Abstract static methods in typescript

Static methods in abstract classes cannot be declared as abstract; attempting to do so results in an error.

The below example code is not allowed as it gives an error, the static modifier cannot be used with an abstract modifier.

abstract class MyInterface {
  static abstract myMethod();
}

It throws ‘static’ modifier that cannot be used with an ‘abstract’ modifier.

Conclusion

In conclusion, we’ve covered abstract classes in TypeScript, including constructors, setter and getter methods, extending classes, and implementing interfaces, with syntax and examples.