In JavaScript, Inheritance is a key concept in object-oriented programming that allows one class to inherit properties and methods from another class.
This enables code reuse, as a subclass (derived class) can inherit the features of a superclass (base class) and extend or override them as needed.
In JavaScript, inheritance is typically implemented using prototype-based inheritance.
Here's an example illustrating inheritance in JavaScript:
// Base class (Superclass) function Vehicle(bodyType) { this.bodyType = bodyType; } // Adding a method to the base class prototype Vehicle.prototype.blowHorn = function() { console.log("Some generic sound"); }; // Subclass (Derived class) function Car(name, bodyType) { // Call the superclass constructor Vehicle.call(this, bodyType); this.name = name; } // Set up prototype chain: Car inherits from Vehicle Car.prototype = Object.create(Vehicle.prototype); Car.prototype.constructor = Car; // Resetting the constructor property // Adding a method to the subclass prototype (overrides the superclass method) Car.prototype.blowHorn = function() { console.log("Pee! Pee!"); }; // Creating instances const yourVehicle = new Vehicle("SUV"); const yourCar = new Car("Defender", "SUV"); // Using inherited methods yourVehicle.blowHorn(); // Outputs: Some generic sound yourCar.blowHorn(); // Outputs: Pee! Pee!
The `Vehicle` function serves as the base class (superclass), representing a generic `vehicle`. It has a `blowHorn` method defined on its prototype.
The `Car` function serves as the subclass (derived class), representing a specific type of vehicle (car). It calls the Vehicle constructor using Vehicle.call(this, bodyType) to initialize the bodyType property inherited from the superclass.
The `Car` prototype is set to a new object created from the `Vehicle` prototype, establishing the prototype chain. This allows instances of `Car` to inherit methods from `Vehicle`.
The blowHorn method in the Car subclass overrides the method with a specific implementation for cars.
Instances of both classes (yourVehicle and yourCar) can use their respective blowHorn methods.
With the introduction of ES6, a more convenient syntax for achieving inheritance was introduced using the class keyword:
class Vehicle { constructor(bodyType) { this.bodyType = bodyType; } blowHorn() { console.log("Some generic sound"); } } class Car extends Vehicle { constructor(name, bodyType) { super(bodyType); // Call the superclass constructor this.name = name; } blowHorn() { console.log("Pee! Pee!"); } } // Creating instances const yourVehicle = new Vehicle("SUV"); const yourCar = new Car("Defender", "SUV"); // Using inherited methods yourVehicle.blowHorn(); // Outputs: Some generic sound yourCar.blowHorn(); // Outputs: Pee! Pee!
With ES6 class syntax, inheritance is more readable and aligns with the class-based inheritance model used in many other programming languages.
The extends keyword is used to establish the subclass relationship, and the super keyword is used to call the superclass constructor.