JavaScript inheritance and overriding
extends connects a subclass to a superclass. A derived constructor must call super before using this so the base initialization can run.
JavaScript
class Animal {
constructor(name) { this.name = name; }
speak() { return this.name + ": sound"; }
}
class Dog extends Animal {
constructor(name) { super(name); }
speak() { return this.name + ": woof"; }
}
for (const animal of [new Animal("Milo"), new Dog("Rex")]) {
console.log(animal.speak());
}