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. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.
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

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());

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"

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);

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);

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.

super(...) basics quiz
Quick check: super in a subclass constructor.

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

Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Inheritance, super, and when to prefer composition” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Inheritance, super, and when to prefer composition”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
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
- Class fields, private # fields, static members
- Inheritance, super, and when to prefer composition
- Mixins (taste) — simple reuse without deep hierarchies