javascript
  1. javascript-inheritance

JavaScript Inheritance

Inheritance refers to the ability to create new classes based on existing classes. It allows the new class to take on the properties and methods of the existing class, while also adding its own unique properties and methods.

Syntax

There are several ways to implement inheritance in JavaScript, but the most common way is through the use of the extends keyword.

class Parent {
  // properties and methods
}

class Child extends Parent {
  // properties and methods
}

In this syntax, the Child class is inheriting from the Parent class, and can access its properties and methods by calling super().

Example

Let's say we have a Person class with two properties, name and age, and a method called speak() that prints out a greeting message. We can create a new class called Student that inherits from Person and adds a new property called grade.

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  speak() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

class Student extends Person {
  constructor(name, age, grade) {
    super(name, age);
    this.grade = grade;
  }

  study() {
    console.log(`${this.name} is studying hard.`);
  }
}

const john = new Student('John', 18, 'A');
john.speak(); // Hello, my name is John and I am 18 years old.
john.study(); // John is studying hard.

Output

Hello, my name is John and I am 18 years old.
John is studying hard.

Explanation

In this example, we created a Student class that inherits from the Person class using the extends keyword. The Student class has a constructor that calls the super() method to inherit the name and age properties from the Person class. It also has a new property called grade.

We then created a new Student object called john and called its speak() method, which prints out a greeting message. We also called its study() method to see that it has a new method.

Use

Inheritance is a powerful tool for code reuse and organization. It allows you to create new classes based on existing ones, reusing the code you’ve already written. This can be especially useful when you have a large codebase or need to create complex objects with many properties and methods.

Important Points

  • Inheritance is the ability to create new classes based on existing classes.
  • Use the extends keyword to inherit from an existing class.
  • Use the super() method to call the parent constructor and access its properties and methods.
  • Inheritance allows for code reuse and organization.

Summary

Inheritance is an important concept in object-oriented programming that allows you to create new classes based on existing ones. It’s a powerful tool for code reuse and organization, and can help you create more complex objects with less code. Use the extends keyword to inherit from an existing class, and the super() method to call the parent constructor and access its properties and methods.

Published on: