Lesson 29 / 38
Inheritance with extends
Building a class on top of another, and calling into the parent.
extends and super
extends links a subclass to a parent; super.method() calls the parent's version from inside the override.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound.`; }
}
class Dog extends Animal {
speak() {
return `${super.speak()} Specifically, a bark!`;
}
}
console.log(new Dog("Rex").speak());Why inherit
extends lets a subclass reuse and extend a parent's behavior instead of duplicating it — the core OOP idea of reuse.
super() must come first
In a subclass constructor, call super(...) before using this — JS enforces this rule strictly.