Types of Inheritance

The term inheritance can be divided into following three parts-

Types of Inheritance in ES6
  • Single-level Inheritance
  • Multiple Inheritance
  • Multi-level Inheritance

Single-level Inheritance

The single-level inheritance can be defined as an inheritance in which the child class can only inherit the properties from one parent class. The single-level inheritance allows us to inherit the properties from the parent class that helps us to increase the reusability of the program code. It also helps us to modify the existing code with the new features. The program code will be less repetitive with the help of inheritance.

Types of Inheritance in ES6

Multiple Inheritance

The Multiple Inheritance can be defined as an inheritance in which a class can inherit the properties from the various classes. ES6 doesn’t support the multiple inheritance.

Types of Inheritance in ES6

Multi-level Inheritance

The Multi-level inheritance can be defined as an inheritance in which a child class inherits the properties from another child class, which inherited from parent class and so on. The multi-level inheritance has multiple base classes.

Types of Inheritance in ES6

Example

Here, we have an example to illustrate the multi-level inheritance.

Output

After the execution of the above example, we got the following output:

Types of Inheritance in ES6

Method Overriding with Class Inheritance

The method overriding is a common feature used in various programming languages. Method overriding is a mechanism used to enable derived class to redefine the methods of its base class.

Rules for defining the method overriding

  • The signature of the method must be same in both (derived and base) classes.
  • The method name also must be same in parent class as well in child class.

Example

We will try to understand the method overriding with the help of an example.

Output

In the above example, the superclass function has been modified in the derived class. So, after the execution of the code, we got the following output:

Types of Inheritance in ES6

Super Keyword

The super keyword is a new feature introduced in ES 2015 (ES6). The super keyword provides a facility for subclass to invoke the data members, properties, and constructor of its superclass.

The super[expr] and super.prop expressions are easily accessible in any method of object literals and classes.

Syntax

Example

In the above example, the sub class extends the properties of its super class. These both classes have their own unique properties. We use the super keyword to import the properties from base class to derived class.

Output

After the execution of the above code, we got the following output:

Types of Inheritance in ES6

Getter and Setter Function

Setter

If there is a need to set the value of a property, we invoke the setter function. The setter function is determined by the set keyword.

Syntax of Setter function

Example

Here, we have an example to illustrate for the same-

Output

After the execution of the above code, we got the following output:

Types of Inheritance in ES6

Getter

If there is a need to fetch the value of a property then we invoke the getter function. The getter function is determined by the get keyword.

Syntax of getter function

Example

Here, we have an example to implement the getter function.

Output

After the execution of the code, we got the following output:

Types of Inheritance in ES6

Pin It on Pinterest

Share This