Lesson 30 / 40

Inheritance with extends

Extend classes with extends and super, reuse parent behaviour and follow the constructor rules.

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.