Tuesday, September 4, 2018

Typescript - Beginner Guide to abstract class with examples

In this Blog Post, We are going to learn the Abstract class tutorials in typescript.
It is one of the concepts of Object Oriented Programming. Other concepts are Classespolymorphism, Encapsulation and Interfaces.
Typescript Abstract Class tutorials with examples

Abstract Class

Typescript supports object-oriented programming concepts using classes, interfaces, and abstract classes. the class is defined with the abstract keyword is called abstract classes. It is used to provide an abstraction 
These may not be created instances, instead of instances created by extending abstract classes. Abstract methods contain a signature, not method implementations. 

Syntax

abstract class abstractclass{
}

Abstract methods 

Methods without implementation or body is called abstract methods. It contains only method signature. abstract methods can be declared by append abstract keyword Implementation of this methods can be done by Classes which extends this abstract class if the abstract method is declared in a class, the class must be declared with abstract keyword. Abstract classes contain abstract methods and nonabstract methods.

Syntax

abstract method(): returntype;

Example 

An abstract class is declared with an abstract method
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
}
Now create an instance of Abstract class, It throws 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

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, you can call all methods of abstract classes
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
employee.getName();

Abstract Properties and get/set methods

An abstract class can also declare abstract member variables and also accessories can be declared abstract methods using set and get accessor typescript.
abstract class Parent {
    abstract name: string;
    abstract get value();
    abstract set value(v: number);
}

Abstract class implements interface

Abstract classes implement Interfaces and need to provide an abstract method, otherwise it gives error Class 'MyClass' incorrectly implements interface 'MyInterface'.


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

}

Abstract static methods

Abstract methods should not be used with static 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();
}

Related article


EmoticonEmoticon