Flat Preloader Icon

JS Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit properties and methods from other classes. It promotes code reuse, extensibility, and modularity by enabling the creation of new classes based on existing ones. In JavaScript, inheritance is achieved through prototype-based inheritance.

Prototype-Based Inheritance:

  • Prototype:
      • Every JavaScript object has an internal property called [[Prototype]] (often accessed via __proto__ or Object.getPrototypeOf()). This [[Prototype]] references another object known as the object’s prototype.
  • Prototype Chain:

    • When a property or method is accessed on an object, JavaScript first looks for it directly on the object. If not found, it looks at the object’s prototype (its [[Prototype]]), and if still not found, it continues up the prototype chain until it reaches the top-level prototype (Object.prototype).
  • Constructor Functions:

    • Constructor functions are used to create objects with a shared prototype. When you create an object using a constructor function with the new keyword, the object’s [[Prototype]] is set to the constructor function’s prototype property.
				
					// Parent class (superclass)
class Animal {
    constructor(name) {
        this.name = name;
    }

    // Method shared by all Animal instances
    speak() {
        console.log(`${this.name} makes a sound.`);
    }
}

// Child class (subclass) inheriting from Animal
class Dog extends Animal {
    constructor(name, breed) {
        super(name);
        // Call the parent class constructor
        this.breed = breed;
    }

    // Method specific to Dog instances
    bark() {
        console.log(`${this.name} barks loudly.`);
    }
}

// Creating instances of the Dog class
let dog1 = new Dog("Buddy", "Labrador");
dog1.speak(); // Output: Buddy makes a sound.
dog1.bark(); // Output: Buddy barks loudly.

				
			

Key Concepts:

  • Superclass and Subclass:

    • The superclass (parent class) is the class being inherited from, and the subclass (child class) is the class inheriting from the superclass.
  • super Keyword:

    • The super keyword is used within the constructor of a subclass to call the constructor of the superclass and pass along any required parameters.
  • Method Overriding:

    • Subclasses can override methods inherited from the superclass by providing their own implementation of the method with the same name.

Benefits of Inheritance:

  • Code Reusability:

    • Inheritance allows you to reuse code by defining common behavior in a superclass and extending or customizing it in subclasses.
  • Abstraction and Modularity:

    • Inheritance promotes abstraction and modularity by organizing related functionality into hierarchical class structures, making code easier to understand and maintain.
  • Polymorphism:

    • Inheritance enables polymorphic behavior, where objects of different subclasses can be treated interchangeably based on their common superclass interface.

Limitations:

  • JavaScript’s prototype-based inheritance can be less intuitive for developers accustomed to classical inheritance models found in languages like Java or C++.
  • Overuse of inheritance can lead to tight coupling between classes, making the code harder to maintain and extend.