TypeScript Abstract Class

In object oriented, abstract classes are base classes and cannot be instantiated directly. Abstract classes can have abstract and non-abstract methods. Methods that are marked as abstract do not have implementation and must be implemented by derived classes or sub classes. Abstract classes and methods can be created using abstract keyword within the abstract class.

Unlike interface:
An abstract class can give implementation details for its members.
An abstract class allows to marks its members methods as private and protected.

TypeScript Getter and Setter Property Example

class classA {
    protected name: string;
    public constructor(name: string) 
     this.name = name;
    public print() {
		return "Hello " + this.name;
	abstract printAbstract(): void; // This must be implemented by derived classes
class classB extends classA {
    constructor(name: string) { super(name); }
	printAbstract(): void {
        console.log("Abstract method implemented by derived class");

let b = new classB("Jimi Scott");

let classA = new classsA(); // error: cannot create an instance of an abstract class
Try it Yourself

In the above example, a new abstract class classA is created. This abstract class has declared one method printAbstract using abstract keyword. That means, this method must be implemented by the derived class. Here, class classB derived from classA and implementing printAbstract abstract method of base class. Also above example demonstrates, when creating object of abstract class error will be resulting.