Inheritance with extends and super

One class can extend another. The child inherits the methods of the parent through the prototype chain, keeps its own prototype for its own methods, and its constructor calls the parent constructor with super. Everything the previous lessons built by hand, this syntax writes in two lines.

This lesson covers the call to super, a method that overrides a parent method and still uses it, the way instanceof reaches through the chain, and the error that a constructor gets when it forgets the call. The lesson on prototypes is the machinery under all of it.

extends and super

class Dog extends Animal sets the prototype of Dog.prototype to Animal.prototype and the prototype of the class itself to the parent class, which is what makes the static members visible too. The constructor of the child calls super(name), which runs the parent constructor on the same object, and then adds its own field.

The file extends.mjs:

class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a sound`; } } class Dog extends Animal { constructor(name, breed) { super(name); this.breed = breed; } } const dog = new Dog('Rex', 'collie'); console.log('the inherited field:', dog.name); console.log('the own field:', dog.breed); console.log('the inherited method:', dog.speak()); console.log('it is both:', dog instanceof Dog, dog instanceof Animal); console.log('the parent class is one step up:', Object.getPrototypeOf(Dog) === Animal);

node extends.mjs

the inherited field: Rex
the own field: collie
the inherited method: Rex makes a sound
it is both: true true
the parent class is one step up: true

The name came from the parent constructor and the breed was set by the child, on one object. The method was found on the parent prototype, and instanceof answered true for both classes because the chain has both prototypes in it. The last line reads the class chain, which is a second chain and the one static members travel along.

Overriding a method

A method with the same name in the child overrides the parent method for the child and its own children, and super.speak() calls the parent version from inside the override. The parent is untouched, so an instance of another subclass or of the parent itself keeps the original behaviour, which is the difference between overriding and editing.

The file override.mjs:

class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a sound`; } } class Dog extends Animal { speak() { return `${super.speak()}, and barks`; } } class Cat extends Animal { speak() { return `${this.name} says meow`; } } const animals = [new Animal('A animal'), new Dog('Rex'), new Cat('Tom')]; for (const animal of animals) { console.log(animal.speak()); } console.log('each class keeps its own method:', Dog.prototype.speak !== Animal.prototype.speak); console.log('an override does not change the parent:', new Animal('Another').speak());

node override.mjs

A animal makes a sound
Rex makes a sound, and barks
Tom says meow
each class keeps its own method: true
an override does not change the parent: Another makes a sound

Three objects and three answers from one loop, because the method is chosen by the object and not by the variable. The dog added a sentence to the parent answer, and the cat replaced it. The last two lines show that the parent prototype still holds its own method, so an override is a new property on a lower prototype rather than a change to the old one.

instanceof across the chain

A chain of three classes answers instanceof three times true, because the operator walks the whole chain. The constructor property comes from the prototype of the instance, so it is the class that was used and not the parent. The two chains are separate: instances follow prototype, and the classes themselves follow the link that static members use.

The file instanceof.mjs:

class A {} class B extends A {} class C extends B {} const c = new C(); console.log('the three answers:', c instanceof C, c instanceof B, c instanceof A); console.log('a plain object is none of them:', {} instanceof A); console.log('the prototype chain is two steps:', Object.getPrototypeOf(C.prototype) === B.prototype); console.log('the constructor is still C:', c.constructor === C); console.log('the class chain is separate:', Object.getPrototypeOf(C) === B);

node instanceof.mjs

the three answers: true true true
a plain object is none of them: false
the prototype chain is two steps: true
the constructor is still C: true
the class chain is separate: true

All three answers were true and the plain object was not an instance of anything, which is the whole of the operator. The constructor property pointed at the bottom class, and the two chain checks show the two links: one between the prototype objects and one between the classes. A static method travels along the second, which the next lesson uses.

super before this

A child constructor does not receive an object of its own: the object arrives from the parent constructor, so super has to run first. Reading or writing this before that call throws a ReferenceError, and so does returning from the constructor without ever calling it. The first class below is wrong on purpose and the second one is written the right way, so a linter reports the first class and accepts the second.

The file superfirst.mjs:

class Animal { constructor(name) { this.name = name; } } class Dog extends Animal { constructor(name) { this.name = name; } } try { new Dog('Rex'); } catch (error) { console.log(error.name + ': ' + error.message); } class Cat extends Animal { constructor(name) { super(name); this.kind = 'cat'; } } console.log('with super first:', new Cat('Tom').name, new Cat('Tom').kind);

node superfirst.mjs

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
with super first: Tom cat

The message names the reason rather than the line, and it appears at the first use of this . With the call in place the constructor works and the field from the parent and the field from the child both exist. A class with no constructor of its own gets one that passes its arguments to super, which is why many subclasses need no constructor at all.

Exercises

  1. Add a third class that extends Dog in extends.mjs and give it one method. Which of the two parent methods does it inherit?
  2. Call super.speak() from the Cat class in override.mjs. What does the loop print for the cat now?
  3. Ask Animal.prototype instanceof Object in instanceof.mjs. What is the answer, and why?
  4. Remove the super(name) call from the working Cat class in superfirst.mjs and keep the write to this.kind. Which error appears, and at which line?
  5. Write a child class with no constructor at all and create it with one argument. Where does the argument go?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does class Dog extends Animal do with the prototypes?

    Answer

    It puts Animal.prototype in the chain of Dog.prototype

    The chain is what inheritance is, and the methods are read through it rather than copied.

  2. What does super(name) do in a child constructor?

    Answer

    It runs the parent constructor on the same object

    The parent body writes on the object under construction, and the child adds to it after.

  3. What does super.speak() mean inside an overriding method?

    Answer

    It calls the parent version of the same method

    The call reaches the prototype above the current one for that name.

  4. When may a child constructor read this?

    Answer

    After the call to super

    The object comes from the parent constructor, so there is no this before that call.

  5. What does this program print? class A { constructor(n) { this.n = n; } go() { return "A" + this.n; } } class B extends A { go() { return "B" + this.n; } } console.log(new A(1).go(), new B(2).go());

    Answer

    A1 B2

    Each class answers with its own method, and the field comes from the shared constructor.

  6. What does this program print? class A { go() { return "a"; } } class B extends A { go() { return super.go() + "b"; } } console.log(new B().go());

    Answer

    ab

    The override calls the parent method and adds its own letter to the answer.

  7. What does this program print? class A {} class B extends A {} class C extends B {} const c = new C(); console.log(c instanceof A, c instanceof C);

    Answer

    true true

    The chain of a C instance holds the prototypes of all three classes.

  8. What does this program print? class A { constructor() { this.kind = "a"; } } class B extends A { constructor() { super(); this.kind = "b"; } } console.log(new B().kind);

    Answer

    b

    The parent set the field first, and the child wrote over it after super returned.

  9. What happens when this module runs? class A {} class B extends A { constructor() { this.n = 1; } } new B();

    Answer

    A ReferenceError: must call super before accessing this

    The object does not exist until the parent constructor runs, so the write to this is refused.

  10. What happens when this module runs? class A { constructor(n) { this.n = n; } } class B extends A { constructor() { super(1); super(2); } } new B();

    Answer

    A ReferenceError, because super may be called only once

    A constructor runs once per object, and a second call to super has no object left to set up.

The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev