
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain sub-classes and inheritance in ES6
In JavaScript, developers used the prototype to inherit the function with another function in ES5. In the ES6, classes introduced in JavaScript can be used for inheritance as other programming language does.
What are sub-classes and inheritance?
As the subclass word represents, it is a child class of another class. We can use inheritance to create or derive a subclass from the superclass, and we can call the class to a superclass from which we derive the class and the sub-class to the derived class.
The subclass contains all the properties and methods of the superclass, and we can access it using the sub-classes object. You can use the "extend" keyword to derive the class from the superclass.
Syntax
You can follow the syntax below to inherit the sub-class from the superclass.
Class superClass { // members and methods of the superClass } class subClass extends superClass { // it contains all members and methods of the superclass // define the properties of the subclass }
We have used the class keyword in the above syntax to create a class. Also, users can see how we can use the extends keyword to inherit the subClass from the superClass.
Benefits of inheritance
Before we proceed with the tutorial, let’s understand the different benefits of inheritance.
The inheritance allows us to reuse the code of the superclass.
The inheritance saves time, as we don’t need to write the same code often.
Also, we can produce maintainable code with the proper structure using inheritance.
We can override the superclass methods using inheritance and implement them again in the sub-class.
Let’s understand inheritance via real-life examples. So we can understand the inheritance properly.
Example
In the example below, we have created the house class. The two_BHK class inherits the house class, which means the Two_BHK class contains all properties and methods of the house class.
We have overridden the get_total_rooms() method of the house class and implemented their own in Two_BHK class.
<html> <body> <h2>Using the <i> extends </i> keyword to inherit classes in ES6 </h2> <div id = "output"> </div> <script> let output = document.getElementById("output"); class house { color = "blue"; get_total_rooms() { output.innerHTML += "House has a default room. </br>"; } } // extended the house class via two_BHK class class Two_BHK extends house { // new members of two_BHK class is_galary = false; // overriding the get_total_rooms() method of house class get_total_rooms() { output.innerHTML += "Flat has a total of two rooms. </br>"; } } // creating the objects of the different classes and invoking the //get_total_rooms() method by taking the object as a reference. let house1 = new house(); house1.get_total_rooms(); let house2 = new Two_BHK(); house2.get_total_rooms(); </script> </body> </html>
Now, you can understand the real use of inheritance. You can observe in the above example how we have reused the code using inheritance. Also, it provides the clear structure that the above example demonstrates. Furthermore, we can define the structure of the method in the super-class and implement it in the sub-class. So, the superclass provides a clear structure of methods, and we can implement them in the sub-class.
Example
In this example, we have used the class's constructor to initialize the class's properties. Also, we have used the super() keyword to invoke the super-class's constructor from the sub-class.
You can keep in mind that you need to write the super() keyword in the sub-class constructor before we initialize any property of the sub-class.
<html> <body> <h2>Using the <i> extends </i> keyword to inherit classes in ES6 </h2> <div id = "output"> </div> <script> let output = document.getElementById("output"); // creating the superclass class superClass { // constructor of the super-class constructor(param1, param2) { this.prop1 = param1; this.prop2 = param2; } } // Creating the sub-class class subClass extends superClass { // constructor of subClass constructor(param1, param2, param3) { // calling the constructor of the super-class super(param1, param2); this.prop3 = param3; } } // creating the object of the subClass let object = new subClass("1000", 20, false); output.innerHTML += "The value of prop1 in the subClass class is " + object.prop1 + "</br>"; output.innerHTML += "The value of prop2 in subClass class is " + object.prop2 + "</br>"; output.innerHTML += "The value of prop3 in subClass class is " + object.prop3 + "</br>"; </script> </body> </html>
We have learned about inheritance in this tutorial. Also, this tutorial taught us to override a method in the subclass and invoke the superclass's constructor from the subclass to initialize all superclass properties.
- Related Articles
- Explain Inheritance vs Instantiation for Python classes.
- Explain the usage of Classes and the Benefits of Inheritance in Swift
- Introduction to Classes and Inheritance in Python
- Explain Constants in ES6
- Explain RegExp in ES6?
- Explain Handler Method in ES6
- Explain page redirection in ES6?
- How we can extend multiple Python classes in inheritance?
- How does class variables function in multi-inheritance Python classes?
- Explain wrapper classes in Java?
- Can you explain what is metaclass and inheritance in Python?
- Explain character classes in Java regular expressions
- What is Inheritance in Java? Explain with an example
- ES6 Features and Syntax
- Explain the sub-expression "[...]" in Java Regular expressions
