---
title: "What is inheritance in JavaScript explain the all inheritance?"  
description: "What is inheritance in JavaScript explain the all inheritance?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93755/what-is-inheritance-in-javascript-explain-the-all-inheritance  
category: "web application"  
tags: ["javascript", "web application development", "web development"]  
reading_time: 3 minutes  

---

# What is inheritance in JavaScript explain the all inheritance?

What is [inheritance](https://www.mindstick.com/articles/80/inheritance-in-c-sharp) in [JavaScript](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [explain](https://yourviews.mindstick.com/view/86025/content-created-for-humans-not-for-bots-explain-this-statement) the all inheritance?

## Answers

### Answer by user

Inheritance is an important concept in object oriented programming. In the classical inheritance, methods from base class get copied into derived class. In JavaScript, inheritance is supported by using prototype object. There are two way for apply inheritance property. 1. Using extends keyword 2. Using prototype function **Using extends keyword**

```
Syntax:
class child-class-name extends parent-class-name{
 constructor(){
  // parent constructor calling using super()
  // variable initialization
 }
}
```

```
Example
<!DOCTYPE html>
<html>
<body>
<p>Use the 'extends' keyword to inherit all methods from another class.</p>
<p>Use the 'super' method to call the parent's constructor function.</p>
<p id='demo'></p>
<script>
class Car {
  constructor(brand) {
    this.carname = brand;
  }
  present() {
    return 'I have a ' + this.carname;
  }
}
class Model extends Car {
  constructor(brand, mod) {
    super(brand);
    this.model = mod;
  }
  show() {
    return this.present() + ', it is a ' + this.model;
  }
}
let myCar = new Model('Ford', 'Mustang');
document.getElementById('demo').innerHTML = myCar.show();
</script>
</body>
</html>
```

**Using prototype function** Let's see how we can achieve inheritance functionality in JavaScript using prototype object. Let's start with the Person class which includes FirstName & LastName property as shown below.

```
<!DOCTYPE html>
<html>
<body>
 <script>
// prototype Parent class
function Person(firstName, lastName) {
  this.FirstName = firstName || 'unknown';
  this.LastName = lastName || 'unknown';
 }
Person.prototype.getFullName = function () {
  return this.FirstName + ' ' + this.LastName;
 }
// Student prototype function
function Student(firstName, lastName, schoolName, grade)
 {
  Person.call(this, firstName, lastName);
  this.SchoolName = schoolName || 'unknown';
  this.Grade = grade || 0;
 }
//Student.prototype = Person.prototype;
Student.prototype = new Person();
Student.prototype.constructor = Student;
var std = new Student('James','Bond', 'XYZ', 10);
document.writeln(std.getFullName()); // James Bond
document.writeln(' <br />'+ std instanceof Student); // true
document.writeln(' <br />'+ std instanceof Person); // true
 </script>
</body>
</html>
```

\

### Answer by Ethan Karla

Inheritance is an important concept in object-oriented programming, In classical inheritance, methods from the base class get copied into the derived class. In JavaScript, inheritance is supported using prototype objects. There is two way for apply inheritance property.

## Features of inheritance

1. inherited the parent property
2. increase code reusability
3. fast accessing
4. improve security

## Using extends keyword

```
Syntax
class child-class-name extends parent-class-name{ constructor(){  // parent constructor calling using super()  // variable initialization }}
```

```
Example<!DOCTYPE html><html><body><p id='demo'></p><script>class Car {  constructor(brand) {    this.carname = brand;  }  present() {    return 'I have a ' + this.carname;  }}
class Model extends Car {  constructor(brand, mod) {    super(brand);    this.model = mod;  }  show() {    return this.present() + ', it is a ' + this.model;  }}let myCar = new Model('Ford', 'Mustang');document.getElementById('demo').innerHTML = myCar.show();</script></body></html>
```

## Using prototype function

```
class_name.prototype.prototype_name= function() {
 statement
};
```

```
<!DOCTYPE html><html><body> <script>    // prototype Parent class	function Person(firstName, lastName) {  this.FirstName = firstName ;  this.LastName = lastName ;             }Person.prototype.getFullName = function () {  return this.FirstName + ' ' + this.LastName; }// Student prototype functionfunction Student(firstName, lastName, schoolName, grade) {  Person.call(this, firstName, lastName);  this.SchoolName = schoolName ;  this.Grade = grade; }//Student.prototype = Person.prototype;Student.prototype = new Person();Student.prototype.constructor = Student;var std = new Student('James','Bond', 'XYZ', 10);document.writeln(std.getFullName()); // James Bond </script>	</body></html>
```

\


---

Original Source: https://answers.mindstick.com/qa/93755/what-is-inheritance-in-javascript-explain-the-all-inheritance

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
