Inheritance is a fundamental concept in object-oriented programming (OOP) languages that enables the creation of a new class based on an existing one. This mechanism allows a class to obtain the attributes and methods of another class. The class that provides its members for inheritance is referred to as the base class, while the class that inherits these members is known as the derived, child, or subclass. In the child class, it is possible to override or alter the behaviors inherited from its parent class.
Prior to ES6, JavaScript relied on functions and prototype-based inheritance; however, TypeScript introduces class-based inheritance derived from the ES6 version. In TypeScript, class inheritance is implemented using the extends keyword. It accommodates only single inheritance and multilevel inheritance, while it does not support multiple or hybrid inheritance.
Syntax
We can declare a class inheritance as below.
class sub_class_name extends super_class_name
{
// methods and fields
{
Why use inheritance?
- We can use it for Method Overriding (so runtime polymorphism can be achieved).
- We can use it for Code Reusability.
Inheritance Example
As illustrated in the figure above, Audi represents a subclass while Car serves as the superclass. The connection between these two classes can be defined as Audi IS-A Car, indicating that Audi is a specific type of Car.
class Car {
Color:string
constructor(color:string) {
this.Color = color
}
}
class Audi extends Car {
Price: number
constructor(color: string, price: number) {
super(color);
this.Price = price;
}
display():void {
console.log("Color of Audi car: " + this.Color);
console.log("Price of Audi car: " + this.Price);
}
}
let obj = new Audi(" Black", 8500000 );
obj.display();
Output:
In the example provided, the Audi class inherits from the Car class through the use of the extends keyword. This indicates that the Audi class has access to all the attributes and methods of the Car class. The constructor of the Audi class not only sets up its own properties but also initializes the attributes of the parent class by utilizing the special keyword 'super.' The super keyword serves the purpose of invoking the constructor of the parent class along with its associated values.
Types of Inheritance
We can classify the inheritance into the five types. These are:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Note: TypeScript supports only single and multilevel inheritance. It doesn't support multiple, hierarchical, and hybrid inheritance.
Single Inheritance
Single inheritance permits the acquisition of attributes and functionality from a maximum of one parent class. This mechanism enables a derived or subclass to take on the properties and behaviors of a base class, promoting code reusability while also allowing for the integration of new features into the existing code. Additionally, single inheritance reduces code redundancy.
Example
class Shape {
Area:number
constructor(area:number) {
this.Area = area
}
}
class Circle extends Shape {
display():void {
console.log("Area of the circle: "+this.Area)
}
}
var obj = new Circle(320);
obj.display()
Output:
Multilevel Inheritance
When a subclass is created from another subclass, this form of inheritance is referred to as multilevel inheritance. Consequently, multilevel inheritance involves multiple parent classes. This concept is analogous to the relationship among Grandfather, Father, and Child.
Example
class Animal {
eat():void {
console.log("Eating")
}
}
class Dog extends Animal {
bark():void {
console.log("Barking")
}
}
class BabyDog extends Dog{
weep():void {
console.log("Weeping")
}
}
let obj = new BabyDog();
obj.eat();
obj.bark();
obj.weep()
Output:
Multiple Inheritance
When an object or class derives attributes and functionalities from multiple parent classes, this form of inheritance is referred to as multiple inheritance. Therefore, multiple inheritance obtains properties from more than one parent class. TypeScript does not facilitate multiple inheritance.
Hierarchical Inheritance
When multiple subclasses are derived from a single base class, this form of inheritance is referred to as hierarchical inheritance. In this scenario, all shared characteristics among the subclasses are encapsulated within the base class. TypeScript does not provide support for hierarchical inheritance.
Hybrid Inheritance
Hybrid inheritance refers to a scenario where a class derives traits and functionalities from multiple forms of inheritance. In simpler terms, it represents a blend of multilevel and multiple inheritance. This can be achieved by integrating more than one inheritance type. However, TypeScript does not accommodate hybrid inheritance.