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

Related Topics

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.