Tsallaka zuwa abun ciki
Justice Tech School

Inheritance with extends and super

extends makes a subclass. super() calls the parent constructor and super.method() calls the parent method. Override methods in the child.

Example
class Animal {
  constructor(n) { this.n = n; }
  speak() { return this.n + " makes a sound"; }
}
class Cat extends Animal {
  speak() { return super.speak() + ": meow"; }
}
console.log(new Cat("Tom").speak());

Exercise

Call the parent constructor.

(name);

Quiz Test

What must a subclass constructor call?

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