JavaScript - Inheritance
JavaScript - Inheritance
Inheritance in JavaScript
The concept ofinheritancein JavaScript allows the child class to inherit the properties and methods of the parent class. Inheritance is also a fundamental concept of object-oriented programming likeencapsulationandpolymorphism.
Sometimes, you must add the properties and methods of the one class into another. For example, you have created a general class for the bike containing the same properties and methods for each bike. After that, you create a separate class for the bike βHondaβ, and you need to add all properties and methods to the βHondaβ class. You can achieve it using inheritance.
Before ECMAScript 6 (ES6), the objectβs prototype was used for inheritance, but in ES6, the βextendsβ keyword was introduced to inherit classes.
The following terminologies are used in this chapter.
- Parent classβ It is a class whose properties are inherited by other classes.
- Child classβ It is a class that inherits the properties of the other class.
JavaScript Single Class Inheritance
You can use the βextendsβ keyword to inherit the parent class properties into the child class. In single class inheritance only a single class inherits the properties of another class.
Syntax
You can follow the syntax below for the single-class inheritance.
class childClass extends parentClass {
// Child class body
}In the above syntax, you can replace the βchildClassβ with the name of the child class and βparentClassβ with the name of the parent class.
Example: Single Class Inheritance
In the example below, the βBikeβ class is a parent class, and the βSuzukiβ class is a child class. The suzuki class inherits the properties of the Bike class.
The Bike class contains the constructor() method initializing the gears property and the getGears() method returning the value of the gears property.
The suzuki class contains the constructor() method to initialize the brand property and getBrand() method, returning the value of the brand property.
We have created an object of the βsuzukiβ class. Using the βsuzukiβ class instance, we invoke the getBrand() and getGears() methods.
Output
The brand of the bike is: Yamaha
Total gears in the bike is: 5In this way, you can use the properties and methods of the parent class through the instance of the child class.
JavaScript super() Keyword
In the above example, we have initialized the βgearβ property of the Bike class with a static value. In real life, you need to initialize it with the dynamic value according to the model of the bike.
Now, the question is how to initialize the properties of the parent class from the child class. The solution is a super() keyword.
The super() keyword is used to invoke the method or access the properties of the parent class in the child class. By default, the super() keyword invokes the constructor function of the parent class. You can also pass the parameters to the super() keyword to pass it to the constructor of the parent class.
Example: Using super() keyword to initialize the parent class properties
In the example below, suzuki class extends the Bike class.
The Bike class contains the constructor, taking gears as parameters and, using it, initializes the gears property.
The βsuzukiβ class also contains the constructor, taking a brand and gears as a parameter. Using the brand parameter, it initializes the brand property and passes the gears parameter as an argument of the super() keyword.
After that, we create an object of the βsuzukiβ class and pass the brand and gears as an argument of the constructor. You can see the dynamic value of the brand and gear property in the output.
Output
The brand of the bike is: Suzuki
Total gears in the bike is: 4In this way, you can dynamically initialize the properties of the parent class from the child class.
JavaScript Multilevel Inheritance
Multilevel inheritance is a type of inheritance in JavaScript. In multilevel inheritance, one class inherits the properties of another class, and other classes inherit current class properties.
Syntax
Users can follow the syntax below for the multilevel inheritance.
class A {
}
class B extends A {
}
class C extends B {
}In the above syntax, the C class inherits the B class, and the B class inherits the A class.
Example
In the example below, the Honda class inherits the Bike class. The Shine class inherits the Honda class.
We use the super() keyword in each class to invoke the parent classβs constructor () and initialize its properties.
We are accessing the properties of the Bike class using the instance of the Shine class, as it indirectly inherits the properties of the Bike class.
Output
The Shine model of the Honda brand has total 5 gears.JavaScript Hierarchical Inheritance
In JavaScript hierarchical inheritance, one class is inherited by multiple classes.
Syntax
The syntax of JYou can follow the syntax below for the hierarchical inheritance.
class A {
}
class B extends A {
}
Class C extends A {
}In the above syntax, B and C both classes inherit the properties of the A class.
Example
In the example below, the Bike class contains the gears property and is initialized using the constructor() method.
The Honda class extends the Bike class. The constructor() method of the Honda class initializes the properties of the Bike class using the super() keyword and model property of itself.
The Suzuki class inherits the Bike class properties. The constructor() method of the Suzuki class also initializes the Bike class properties and the other two properties of itself.
After that, we create objects of both Honda and Suzuki classes and access their properties.
Output
Honda Bike Object: {"gears":5,"model":"Shine"}
Suzuki Bike Object: {"gears":6,"model":"Zx6","color":"Blue"}Inheriting Static Members of the Class
In JavaScript, you can invoke the static methods of the parent class using the super keyword in the child class. Outside the child class, you can use the child class name to invoke the static methods of the parent and child class.
Example
In the example below, the Bike class contains the getDefaultBrand() static method. The Honda class also contains the Bikename() static method.
In the Bikename() method, we invoke the getDefaultBrand() method of the parent class using the βsuperβ keyword.
Also, we execute the Bikename() method using the βHondaβ class name.
Output
The bike name is: Yamaha, X6When you execute any method using the βsuperβ keyword in the multilevel inheritance, the class finds the methods in the parent class. If the method is not found in the parent class, it finds in the parentβs parent class, and so on.
JavaScript Prototype Based Inheritance
You can also update or extend the prototype of the class to inherit the properties of the multiple classes to the single class. So, it is also called multiple inheritance.
Syntax
You can follow the syntax below to use prototype-based inheritance.
Child.prototype = Instance of parent classIn the above syntax, we assign the parent class instance to the child objectβs prototype.
Example: JavaScript Prototype Based Inheritance
In the example below, Bike() is an object constructor that initializes the brand property.
After that, we add the getBrand() method to the prototype of the Bike() function.
Next, we have created the Vehicle() object constructor and instance of the Bike() constructor.
After that, we update the prototype of the Vehicle class with an instance of the Bike. Here, Vehicle works as a child class and Bike as a parent class.
We access the getBrand() method of the Bike() functionβs prototype using the instance of the Vehicle () function.
Output
Bike brand: Yamaha
Bike Price: 100000You canβt access the private members to the parent class in the child class.
Benefits of Inheritance
Here, we will learn the benefits of the inheritance concept in JavaScript.
- Code reusabilityβ The child class can inherit the properties of the parent class. So, it is the best way to reuse the parent class code.
- Functionality extensionβ You can add new properties and methods to extend the parent class functionality in each child class.
- Code maintenanceβ It is easier to maintain a code as you can divide the code into sub-classes.
- Multilevel and hierarchical inheritance allows you to combine data together.