Inheritance and Polymorphism

Inheriting from a Base Class

Inheritance is a powerful concept in object-oriented programming that allows you to create new classes that are based on existing classes. This means that you can reuse the code from the existing class, and you can also extend the existing class with new functionality.

In JavaScript, you can inherit from a base class using the extends keyword. For example, the following code defines a base class called Animal:

class Animal {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log("Hello, my name is " + this.name);
  }
}

The Animal class defines a constructor function and a method called greet().

The following code defines a subclass called Dog that inherits from the Animal class:

class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }

  bark() {
    console.log("Woof!");
  }
}

The Dog class inherits the constructor function and the greet() method from the Animal class. It also defines a new method called bark().

Using ‘extends’ and ‘super’ Keywords

The extends keyword is used to inherit from a base class. The super keyword is used to access the methods and properties of the base class.

For example, the Dog class uses the super keyword to access the constructor function and the greet() method from the Animal class.

Method Overriding and Polymorphism

Method overriding is a concept in object-oriented programming that allows you to redefine the behavior of a method in a subclass. This can be useful if you want to change the behavior of a method in a subclass without affecting the behavior of the method in the base class.

Polymorphism is a concept in object-oriented programming that allows you to treat objects of different types in the same way. This is achieved by using methods that can be overridden in subclasses.

For example, the Dog class overrides the greet() method from the Animal class. The greet() method in the Dog class prints a message that says “Woof! My name is ” + this.name.

Multiple Inheritance with Mixins

Multiple inheritance is a concept in object-oriented programming that allows you to inherit from multiple base classes. This can be useful if you want to combine the functionality of multiple classes into a single class.

In JavaScript, multiple inheritance is not supported natively. However, you can achieve multiple inheritance using mixins.

A mixin is a class that contains a set of methods and properties that can be mixed into other classes. To use a mixin, you need to import the mixin into the class that you want to mixin it into.

For example, the following code defines a mixin called Flying:

class Flying {
  fly() {
    console.log("I'm flying!");
  }
}

The Flying mixin defines a method called fly().

The following code defines a class called Bird that inherits from the Animal class and mixes in the Flying mixin:

class Bird extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }

  fly() {
    console.log("I'm flying!");
  }
}

The Bird class inherits the constructor function and the greet() method from the Animal class. It also mixes in the fly() method from the Flying mixin.

Conclusion

Inheritance, method overriding, polymorphism, and multiple inheritance are all important concepts in object-oriented programming in JavaScript. By understanding these concepts, you can create powerful and reusable objects.