Skip to content

Class Inheritance

ES6 classes provide a cleaner syntax for inheritance. The extends keyword sets up the prototype chain automatically, and super calls the parent class.

class Animal {
constructor(name) {
this.name = name;
}
eat() {
return `${this.name} is eating`;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // calls Animal constructor
this.breed = breed;
}
bark() {
return `${this.name} says woof!`;
}
}
const rex = new Dog('Rex', 'Labrador');
console.log(rex.eat()); // 'Rex is eating' — inherited
console.log(rex.bark()); // 'Rex says woof!' — own
class Animal {
speak() {
return 'Some sound';
}
}
class Dog extends Animal {
speak() {
return 'Woof!'; // overrides parent
}
}
const dog = new Dog();
console.log(dog.speak()); // 'Woof!'
class Animal {
speak() {
return 'Some sound';
}
}
class Dog extends Animal {
speak() {
const parentMessage = super.speak(); // call parent
return `${parentMessage} → Woof!`; // extend it
}
}
const dog = new Dog();
console.log(dog.speak()); // 'Some sound → Woof!'
class Animal {
constructor(name) { this.name = name; }
eat() { return `${this.name} is eating`; }
}
class Mammal extends Animal {
constructor(name, hasFur) {
super(name);
this.hasFur = hasFur;
}
walk() { return `${this.name} is walking`; }
}
class Dog extends Mammal {
constructor(name, breed) {
super(name, true); // Mammals have fur
this.breed = breed;
}
bark() { return `${this.name} says woof!`; }
}
const rex = new Dog('Rex', 'Lab');
console.log(rex.eat()); // Animal method
console.log(rex.walk()); // Mammal method
console.log(rex.bark()); // Dog method
  • Must call super() before accessing this in a subclass constructor
  • If a subclass has a constructor, super() is required
  • Without a constructor, super() is automatically called
  • extends sets up prototype chain automatically
  • super() calls parent constructor
  • super.method() calls parent method
  • Method overriding is straightforward
  • Multi-level inheritance works through the chain