Javatpoint Logo
Javatpoint Logo

TypeScript Inheritance

Inheritance is an important aspect of OOPs languages. It is the ability of a program which creates new classes from an existing class. It is a mechanism that acquires the properties and behaviors of a class from another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived/child/subclass. In child class, we can override or modify the behaviors of its parent class.

Before ES6, JavaScript uses functions and prototype-based inheritance, but TypeScript which comes from ES6 supports the class-based inheritance. The TypeScript uses class inheritance through the extends keyword. TypeScript supports only single inheritance and multi-level inheritance. It doesn't support multiple and hybrid inheritance.

Syntax:

Why use inheritance?

  • For Method Overriding (so runtime polymorphism can be achieved)
  • For Code Reusability

Inheritance Example:

TypeScript Inheritance

As displayed in the above figure, Audi is the subclass and Car is the superclass. The relationship between the two classes is Audi IS-A Car. It means that Audi is a type of Car.

Output:

Color of Audi car:  Black
Price of Audi car: 8500000

In the above example, the Audi class extends the Car class by using the extends keyword. This means that the Audi class can include all the members of the Car class. The constructor of the Audi class initializes its own members as well as the parent class's properties by using a special keyword 'super.' The super keyword is used to call the parent constructor and its values.

Types of Inheritance

Inheritance can be classified into the following categories. TypeScript supports only single and multilevel inheritance. It doesn't support multiple, hierarchical and hybrid inheritance.

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
TypeScript Inheritance

Single Inheritance

Single inheritance can inherit properties and behavior from at most one parent class. It allows a derived/subclass to inherit the properties and behavior of a base class that enable the code reusability as well as we can add new features to the existing code. This makes the code less repetitive.

Example:

Output:

Area of the circle: 320

Multilevel Inheritance

A class which has more than one parent class is known as multilevel inheritance. It is similar to the relation between Grandfather, Father, and Child.

Example:

Output:

Eating
Barking
Weeping

Multiple Inheritance

A class which derived from more than one parent class is known as multiple inheritance. TypeScript does not support multiple inheritance.

Hierarchical Inheritance

When more than one subclass is inherited from a single base class, then this type of inheritance is known as hierarchical inheritance. TypeScript does not support hierarchical inheritance.

Hybrid Inheritance

It is the combination of multilevel and multiple inheritance. It is implemented by combining more than one type of inheritance. TypeScript does not support hybrid inheritance.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA