Typescript abstract class | Beginner tutorial & examples

Typescript Abstract class examples

Typescript Abstract class: Learn with examples.

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

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

What is Abstract Class in Typescript

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

Class is 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{  

abstract keyword added to make it an abstract class.

It contains only abstract methods.

What are Abstract methods in typescript>

Methods without implementation or body are called abstract methods.

It contains only method signature.

abstract methods can be declared by append abstract keyword.

These methods can be implemented by classes that extend the abstract class.

However, if the abstract method is declared in a class, the class must be declared with the abstract keyword.

Abstract classes contain abstract methods and non-abstract methods.

Here is an abstract method syntax

abstract method(): returntype;  

Let us see examples of abstract classes with abstract and non-abstract methods.

Following is an abstract, contains abstract method -getRole() and non abstract 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 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

An abstract class can be extended by subclasses like normal classes and need to provide an implementation for abstract methods. In the following example, Created an instance of a subclass, provided implementation for abstract classes methods.

class HrEmployee extends Employee {  
    constructor(public name: string,public role: string) {  
        super(name,role); // Construtor 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  

How to define abstract setter and getter in class?

An abstract class can also declare abstract member variables and also accessories such as setter and getter can be declared as abstract methods using set and get accessor typescript.

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 and need to provide an abstract method, otherwise it gives error Class ‘MyClass’ incorrectly implements interface‘MyInterface’.

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

How to declare Abstract static methods in typescript

Abstract methods should not be used with 1 modified.

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 {  
   abstract static myMethod();  

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


Learned about abstract class and methods with syntax and examples

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.