Updated April 11, 2023
Definition of TypeScript Abstract Class
Abstract class is the concept of object-oriented programming language. Abstract class is used when we want to give a specific implementation of our methods. In TypeScript, we can create abstract class by simply using ‘abstract’keyword with it. Inside this, we can define our methods that implementation needs to be provided when implemented. In the coming section, we will discuss more abstract class in detail for better understanding and its usage.
Syntax:
As we discussed that abstract classes are used to implement the concept of abstraction from an object-oriented programming language. To define them we use ‘abstract’ keyword. Let’s see its syntax in details for better understanding while doing programming see below;
abstract class class_name {}
As you can see in the above lines of syntax we are using the ‘abstract’ keyword before the class keyword. Inside this class, we can define our methods and variable we want. It is just like the normal class in TypeScript but the difference of ‘abstract’ keyword. Let’s see one practice syntax for a better understanding of abstract class in TypeScript see below;
e.g. :
abstract class DemoABS {}
In this way, we can define it. In the coming section, we will discuss more how it works internally and where we should use this to make our code more reusable in detail.
How abstract class works in TypeScript?
As now we already know that abstract class in TypeScript is used to achieve abstraction in TypeScript, this works in the same way as java. We can define any class as abstract class using ‘abstract’ keyword before the class identifier in TypeScript. Inside this class we can define our method signature, after this, we can extend this class to use its methods to have their own implementation. In this section we will first see a sample example where we will understand its internal working in detail see below;
e.g. :
abstract class DemoABS {
constructor() {
}
getmsg(): void{
console.log("i am abstract class method");
}
}
class DemoNor extends DemoABS {
}
let obj: DemoNor = new DemoNor();
obj.getmsg();
In the above lines of code, you can see we are trying to use abstract class in TypeScript. First, we have created one class named ‘DemoABS’ this will be our abstract class. Inside this class, we have defined one constructor and one method named getmsg(). Both these does not accept any parameter. we have used no parameterized constructor and no argument method here just to demonstrate the use of abstract class in detail. After this, we are creating one more class that is going to use as an abstract class. To use the abstract class methods inside other class we have to extend the class using the ‘extends’ keyword just like java. After this, we have to provide implementation to all the methods available in the abstract class. If we do not provide implementation it will give us a compile-time error. To call the abstract class method we have to create an object of the other class because we cannot create an object of abstract class in an object-oriented programming language. So in the next line, we are creating an object of the DemoNor class which is extending DemoABS class, then only we can call getmsg() method available in an abstract class. So in this way, we can give or provide different implementations to the same method which helps in code optimization and reuse of code.
In the real-life case, we have so many cases where we can implement an abstract class concept. Suppose we have a bank which is a parent bank for all other banks. But in all the bank operations and features are the same, which can be named as withdrawal, deposit, etc. This operation would be the same for all the banks. Also, the bank provides the rate of interest which are different and may vary according to a different bank, so if we have one method named as rateOfinterest() in the abstract class then all the other bank class which are implementing this abstract class can define their own rateOfinterest() so this made it reusable and easy to handle.
Rules and regulation for abstract class
We have so many rules and regulation to define abstract class in any programming language see below;
1) To define abstract class we have to use ‘abstract’ keyword before the class identifier in TypeScript.
2) Abstract class can contain both the methods with and without a body in TypeScript. These are said to be concrete methods in general.
3) If the abstract class contains any abstract method whose body is not defined then e have to give the implementation to the methods when we extend the abstract class in any other class. In short, any class who is extending the abstract class it has to provide implementation to all the methods from the abstract class, or else it has to define itself abstract if all are not implemented.
4) We can also have concrete methods inside the abstract class, this methods do not require any implementation, we can directly access them using the child class object in TypeScript.
5) Most important we cannot create instance of the abstract class, to access its methods we have to extend it.
6) They are very easy to maintain, readable, and easy to understand by the other developer it makes the code clean and divide them in layers.
Examples
1) In this example we are trying to use an abstract class. We have defined two methods one is concrete and the other is abstract method which accepts one parameter. Also, we have several classes that implement the abstract class ‘DemoABS’, this is a sample example for beginners to understand the concept of abstract class in detail for better understanding and use while programming.
Example:
abstract class DemoABS {
constructor() {
}
getmsg(): void{
console.log("i am abstract class method");
}
abstract getValue(string);
}
class DemoNor1 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 1 class ");
}
}
class DemoNor2 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 2 class ");
}
}
class DemoNor3 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 3 class ");
}
}
class DemoNor4 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 4 class ");
}
}
class DemoNor5 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 5 class ");
}
}
let obj1: DemoNor1 = new DemoNor1();
obj1.getmsg();
obj1.getValue("hello from first object here !!")
let obj2: DemoNor1 = new DemoNor1();
obj2.getmsg();
obj2.getValue("hello from second object here !!")
let obj3: DemoNor1 = new DemoNor1();
obj3.getmsg();
obj3.getValue("hello from third object here !!")
let obj4: DemoNor1 = new DemoNor1();
obj4.getmsg();
obj4.getValue("hello from fourth object here !!")
let obj5: DemoNor1 = new DemoNor1();
obj5.getmsg();
obj5.getValue("hello from fifth object here !!")
Output:
Conclusion
Abstraction can be achieved by using abstract class in TypeScript, by the use of it we can reuse our code and provide a different implementation of the same method available. This can be useful when we have different implementations of the same thing in programming.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Abstract Class” was beneficial to you. You can view EDUCBA’s recommended articles for more information.