How to implement inheritance in ES6 ?
We use the extends keyword to implement the inheritance in ES6. The class to be extended is called a base class or parent class. The class that extends the base class or parent class is called the derived class or child class. The super() method in the constructor is used to access all parent’s properties and methods that are used by the derived class.
The below example will demonstrate the use of the extends and super keywords.
Example: Let us create a Shop class that will inherit the methods from the parent “Mall” class.
- In the above example, the super keyword is used as a “function” which calls the parent class Mall with the parameter passed to Shop. This is a key step to be carried out in order to make sure that Shop is an instance of Mall.
- On the other hand, extends keyword is used to set Shop as a subclass or child class of Mall. The use of extends is a must to set some class as a child of other class.