By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > How to Implement Class Inheritance in JavaScript?
JavaScript

How to Implement Class Inheritance in JavaScript?

Admin
Last updated: 2022/12/24 at 3:31 PM
Admin
Share
3 Min Read
How to Implement Class Inheritance in JavaScript

In this article, we will discuss “How to Implement Class Inheritance in JavaScript?”. Since JavaScript is object-oriented programming language and inheritance is a basically useful in object-oriented language, so it is the most probable question. So let’s discuss a bit about it.

Contents
Solution:Output:You may also like:

Solution:

Class inheritance is a pretty basic concept which enables you to define a class which take all functionality from the parent class. Inheritance is useful, when we need to make a class reusable and to increase reusability and to decrease code length. Let’s understand with example code:

<script>
        class Car{
            constructor(name, model){
                this.name = name;
                this.model = model;
            }
            start(){
                console.log(this.name);
            }
            end(){
                console.log("Car stopped!!!");
            }
        }

        class electric extends Car{
            dashboard(){
                console.log("Child Method");
            }
            start(){
                super.start();
                super.end();
                this.dashboard();
            }
        }
        evCar = new electric('Tesla', 'A320');
        evCar.start();
    </script>

Here, we have created a basic class named Car, where we have created a constructor with two arguments, name and model. Then we created two properties using this keyword, then again we added methods named start and end with some console.log().

Now let’s inherit this Car class to electric class, To inherit and use car classes elements in electric class using extends keyword. Also, we have added a method which is method of electric class. Remember that, Child class’s method cannot be accessed in parent class, but elements of parent class can be accessed in child classes.

To get access to the methods from parent class, we will use super keyword. Here we inherited start() and end() methods, which are created in parent class, and now it can be accessed in child class. Lastly, we have created an object of electric class named evCar with two arguments, and we just called start() method. Also, note that, you can make object of parent class which only can access methods of parent class.

Output:

Tesla
Car stopped!!!
Child Method

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Find Duplicate Elements in a Given Array?
  • How to Find The Count of Duplicates in an Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Swap Two Numbers Without A Third Variable How to Swap Two Numbers Without A Third Variable in JS
Next Article How to Define a Class With Properties and Methods in JavaScript How to Define a Class With Properties and Methods in JavaScript?
2 Comments 2 Comments
  • Pingback: How to Find Duplicate Elements in a Given Array? - rocoderes
  • Pingback: How to Find The Count of Duplicates in an Array? - rocoderes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?