Types of Inheritance in ES6

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.

class Animal
{
          eat()
{
          console.log(“eating”);
}
}
class Lion extends Animal
{
          roar()
{
          console.log(“roaring”);
}
}
class BabyLion extends Lion
{
          weep()
{
          console.log(“weeping”);
}
}
var a = new BabyLion();
a.eat();
a.roar();
a.weep();

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.

‘use strict’
class Base
{
          display ()
{
          console.log (“The display () method form Base class”);
}
}
class Derived extends Base
{
          display ()
{
          console.log (“The display () method form Derived class”);
}
}
var object = new Derived ();
object.display();

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

Super(arguments);

Example

‘use strict’
class Base
{
          display ()
{
          console.log (“The display () method form Base class”);
}
}
class Derived extends Base
{
          display ()
{
          super.display();
console.log (“The display () method form Derived class”);
}
}
var object = new Derived ();
object.display();

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

set prop(value)
{
          ( . . . )
}
get[expression] (value)
{
          ( . . . )
}

Example

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

Class Student
{
          constructor (rno, fname, lname)
{
          this.rno  = rno
          this.fname = fname
          this.lname = lname
          console.log(“constructor”)
}
set rollno (new rollno)
{
          console.log(“setter function”)
          this.rno = new rollno
}
}
let Student1 = new Student (‘001’, ‘Thompson’, ‘David’)
console.log(Student1)                                     // Setter function is invoked
student.rollno = 002
console.log(Student1)

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

get prop(value)
{
          ( . . . )
}
get[expression] (value)
{
          ( . . . )
}

Example

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

Class Student
{
          constructor (rno, fname, lname)
{
          this.rno  = rno
          this.fname = fname
          this.lname = lname
          console.log(“constructor”)
}
get fullName ()
{
          console.log (“getter function”)
          return this.fname + “ -” + this.lname
}
}
let Student1 = new Student (‘001’, ‘Thompson’, ‘David’)
console.log(Student1)                                    
console.log(Student1.fullName)                                   // Getter function is invoked

Output

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

Types of Inheritance in ES6