Tsallaka zuwa abun ciki
Justice Tech School

Class Inheritance

extends makes a class inherit from another. Call super() in the constructor to run the parent constructor, and super.method() to reuse a parent method. A child can override methods.

Example
class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return this.name + " makes a sound";
  }
}
class Dog extends Animal {
  speak() {
    return super.speak() + ": woof";
  }
}
console.log(new Animal("Cat").speak());
console.log(new Dog("Rex").speak());

Exercise

Make Dog inherit from Animal.

class Dog  Animal {}

Quiz Test

What does super() do in a constructor?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.