Abstract Class in TypeScript

Saurabh Kumbhar
2 min readApr 7, 2020

--

Abstract class in Typescript acts as a base class that may be derived by other classes. Abstract class typically contains one or more abstract methods or declarations that will be implemented by the class which extends it.

Typescript allows us to define an abstract class using keyword ‘abstract’ .

Abstract Class

An abstract class is a class which may have some methods which are unimplemented . These methods are called abstract methods. We cannot create an instance of an abstract class. But other classes can derived from abstract class and reuse the contents of the base class. So, the main advantage of abstract class is code reusability.

In the given example, MedicineABC class is derived class from the base class i.e. abstract class Medicines. Derived class retains all the properties and declarations present in the base abstract class and also may add some of its own.

Derived Class

As we know, we cannot create instances of abstract class, but can create instances of the derived class. Therefore, while creating instances of MedicineABC you can access all declared from the abstract class also. This tends to code reusability and give you much more standardised code.

Instances of Derived Class
  • Note : Interface vs Abstract Class-

While learning interface and abstract class, they both seems to be quite similar concepts; But they are not.

Interface cannot have implementations of any of its methods but Abstract class have for same its methods.

A class can inherit from multiple interfaces at the same time but a class cannot inherit from multiple classes at the same time.

Interface should be fully implemented but abstract class can partially, fully or ot implemented.

Thank You!

--

--