Back to Collection Challenge
OOP Studio Ages 9-15

Inheritance

Students learn how one class can extend another class and add its own special behavior.

15-20 min Ages 9-15 OOP JavaScript

Inheritance

Inheritance is a family tree for classes. The parent class teaches the basics — every GameCharacter can introduce itself. A child class extends the parent: a Wizard gets everything a GameCharacter has, plus its own spell.

GameCharacter introduce() 🧙 Wizard + castSpell() ðŸĪš Knight + swingSword()
  1. 1

    The parent teaches the basics

    class GameCharacter handles what EVERY character needs — a name and introduce(). Write it once, up at the top of the family tree.

  2. 2

    extends = "is a kind of"

    class Wizard extends GameCharacter means a Wizard is a GameCharacter. It inherits introduce() for free and adds its own castSpell().

  3. 3

    Try it

    Add a Knight extends GameCharacter with a swingSword() method. Both children can introduce themselves — neither had to re-learn it.

Code Editor

Make a special kind of object.

Example code
class GameCharacter {
  constructor(name) {
    this.name = name;
  }

  introduce() {
    say("I am " + this.name + ".");
  }
}

class Wizard extends GameCharacter {
  castSpell() {
    say(this.name + " casts a sparkle spell!");
  }
}

let hero = new Wizard("Nova");
hero.introduce();
hero.castSpell();
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.