Implement class inheritance in javascript

In JavaScript, class inheritance is implemented using the extends keyword. The child class inherits properties and methods from its parent class, and it can also add its own properties and methods.

 
class Animal {
  constructor(name) {
    this.name = name;
  }
  
  speak() {
    console.log(this.name + ' makes a noise.');
  }
}

class Dog extends Animal {
  speak() {
    console.log(this.name + ' barks.');
  }
}

let dog = new Dog('Rex');
dog.speak(); // output: "Rex barks."

 

In the example above, the Dog class extends the Animal class using the extends keyword. This means that the Dog class inherits all the properties and methods of the Animal class.

The speak() method is overridden in the Dog class, which means that it replaces the speak() method inherited from the Animal class. When you call dog.speak(), the overridden speak() method in the Dog class is executed, and the output is "Rex barks."