0Pricing
JavaScript Academy · Lesson

Inheritance, super, and when to prefer composition

Extend a base class with extends, call super in constructors, override methods safely, and know when simple composition is better.

Inheritance, super, and when to prefer composition is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Big picture

Goal: Use basic inheritance without confusion.

  • extends makes a child class
  • super(...) calls the parent constructor
  • Override a method; optionally call super.method()
  • If it is not an “is-a”, prefer composition
Inheritance, super, and when to prefer composition — illustration 1

extends + super

Create a child with extends; call super(name) to run the parent constructor and set up this.

// Base class with a small field and method
class Animal {
  constructor(name) {
    this.name = name;
  }
  info() {
    return "Animal:" + this.name;
  }
}

// Child class extends the base and calls super(...)
class Dog extends Animal {
  constructor(name, breed) {
    // must call super() before using this
    super(name);
    this.breed = breed;
  }
  speak() {
    return "woof";
  }
}

const d = new Dog("Milo", "Beagle");
console.log(d.info(), d.speak());
Inheritance, super, and when to prefer composition — illustration 2

Method override

When overriding, you can call super.method() to reuse base logic, then add your tweak.

// Override a method; keep parent behavior with super.method()
class Button {
  click() {
    return "base-click";
  }
}

class IconButton extends Button {
  click() {
    // call parent behavior, then add something
    const base = super.click();
    return base + "+icon";
  }
}

const b = new IconButton();
console.log(b.click()); // "base-click+icon"
Inheritance, super, and when to prefer composition — illustration 3

Prefer composition when has-a

If the relation is has-a (uses a helper), prefer composition over inheritance to keep classes simple.

// Composition: use a helper object instead of extending
class Logger {
  log(msg) {
    console.log("[log]", msg);
  }
}

class Player {
  constructor(name, logger) {
    this.name = name;
    this.logger = logger; // has-a logger
  }
  score(points) {
    this.logger.log(this.name + " +" + points);
  }
}

const logger = new Logger();
const p = new Player("Ayla", logger);
p.score(5);
Inheritance, super, and when to prefer composition — illustration 4

is-a vs has-a

Inheritance models an is-a relationship: a Dog is an Animal. Composition is for has-a helpers.

// Use instanceof for a quick is-a check
console.log("Dog is Animal?", d instanceof Animal);
console.log("Player is Animal?", p instanceof Animal);
Inheritance, super, and when to prefer composition — illustration 5

Beginner habits

Tips:

  • Keep hierarchies shallow for beginners.
  • Call super(...) first in subclass constructors.
  • Override only what you need; reuse with super.method().
  • Prefer composition for utilities and cross-cutting features.
Inheritance, super, and when to prefer composition — illustration 6

super(...) basics quiz

Quick check: super in a subclass constructor.

Inheritance, super, and when to prefer composition — illustration 7

Recap

Recap: Use extends for true is-a, call super(...) early, override carefully (call super.method() when helpful), and prefer composition for helpers.

Inheritance, super, and when to prefer composition — illustration 8

Frequently asked questions

Is the “Inheritance, super, and when to prefer composition” lesson free?

Yes — the full text of “Inheritance, super, and when to prefer composition” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Inheritance, super, and when to prefer composition”?

Extend a base class with extends, call super in constructors, override methods safely, and know when simple composition is better. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Inheritance, super, and when to prefer composition” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Class fields, private # fields, static members
  2. Inheritance, super, and when to prefer composition
  3. Mixins (taste) — simple reuse without deep hierarchies
← Back to JavaScript Academy