GAME DEV 2026 · OBJECT-ORIENTED JAVASCRIPT

t1a14 — Class Extensions by Innes

Inheritance lets a new class reuse properties and methods from an existing class. Here, myCat extends myAnimal.

Example class code

class myAnimal {
  constructor(passName, passAge) {
    this.myName = passName;
    this.myAge = passAge;
  }

  myShow() {
    // Display this animal's name and age
  }
}

Example extending class code

The cat inherits the name and age properties, then overrides myShow() with cat-specific output.


Why use super()?

When a child class defines its own constructor, it must call super() before using this. This runs the parent constructor and initializes inherited properties.

class myDog extends myAnimal {
  constructor(passName, passAge, passBreed) {
    super(passName, passAge);
    this.myBreed = passBreed;
  }
}

This is another child class example: it inherits the animal's name and age, then adds a breed.

Output

Choose a class example above to see the result.

Extension Challenge

Read the MDN guide to class inheritance. Add a second subclass with its own property and overridden method. Explain where super() is needed and why.