By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
Notification Show More
Latest News
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Create a Non-duplicated Collection
JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript
Aa
  • 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
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

Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
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?