Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1248

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T06:23:08+00:00 2024-11-25T06:23:08+00:00

Day 16 of 30 of JavaScript

  • 62k

Hey reader👋 Hope you are doing well😊
In the last post we have seen about this keyword in JavaScript. In this post we are going to know about Inheritance in JavaScript, we are going to start from very basic and take it to advanced level.
So let's get started🔥

What is Inheritance?

Inheritance is a way through which properties and methods defined in a class can be easily be used by other class.
The class which inherits the properties and methods is called Subclass. And the class whose properties and methods are inherited is called Superclass.
Suppose we have a animal class which has properties such as name, type, sound and has methods such as canMove(), canFly() etc. now we have two different classes namely dog and bird these both classes can inherit from the animal class and can utilize the properties and methods defined in it.

How Inheritance is performed in JavaScript?

To create a class inheritance, use the extends keyword.
Image description
So here we have a Person class which is a super class for Kid class. The Person class is inherited using extends keyword. Now we know that a kid can hav name, gender and age which are already defined in Person class so there is no need to define them seperately in Kid class we can use them with the help of inheritance. We have used super(name,gender,age) here this basically means that we are calling the constructor of Person class from Kid class.
By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods.
We can define Kid specific properties and methods in Kid class.
Image description
Multiple classes can inherit from single class.

Types of Inheritance used in JavaScript

1. Prototypal Inheritance

Objects inherit from other objects through their prototypes.
Image description
Here we have an Animal constructor function then we have added a speak() method to animal prototype then we have a Dog constructor function that calls Animal constructor function using call() method so that we can make use of name property defined in Animal. Then we have created a new object that inherits from Animal.prototype and assigned it to Dog.prototype. This sets up the inheritance chain, allowing instances of Dog to access methods defined on Animal.prototype. The Dog.prototype.constructor = Dog line ensures that the constructor property of Dog.prototype points back to Dog. Becuase after the inheritance setup, if we create an instance of Dog and inspect its prototype chain, we would find that its constructor points to Animal, which is not accurate. The Dog.prototype.constructor = Dog line explicitly sets the constructor property back to Dog, ensuring that instances of Dog correctly reference Dog as their constructor.

2. Classical Inheritance

Introduced in ECMAScript6 (ES6) with the class keyword. Uses a class-based approach similar to other programming languages like Java or C++.Image description

3. Functional Inheritance

Objects inherit properties and methods from other objects through function constructors. It uses functions to create objects and establish relationships between them.
Image description
We have used createAnimal function to create animal object which has name as property and sound as method then we have createDog function that creates a Dog object by calling createAnimal() to get a base animal object. A new dog object is created with the name “Buddy” and breed “Labrador” by calling the createDog function.

So this is how Inheritance is performed using JavaScript. I hope you have understood this blog. Don't forget to like the blog and follow me.
Thankyou🩵

beginnersjavascripttutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.