
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 abstract
keyword was 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 the method signature
, and does not provide an implementation of a method
abstract methods can be declared by appending the 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, 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
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, 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 the Abstract class throws an error. Since new object creation calls the constructor and initializes the constructor.
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?
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 the 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
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 {
static abstract myMethod();
}
It throws ‘static’ modifier that cannot be used with an ‘abstract’ modifier.
Conclusion
Learned about abstract class,
- constructors,
- setter and getter methods
- extend and interface implements with syntax and examples