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.
Why use 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.
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 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.
Area of the circle: 320
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.
Eating Barking Weeping
A class which derived from more than one parent class is known as multiple inheritance. TypeScript does not support multiple 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.
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.