Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Classes  ›  Lesson

Inheritance and super

Inheritance 12 minclass Child extends Parent inherits Parent's methods; super(...) in a subclass constructor must run before this is used, calling the parent's constructor, and super.method() calls the parent's version of an overridden method.

Inheritance and super

In short: class Child extends Parent makes Child inherit every method Parent has — an instance of Child is also, genuinely, an instance of Parent. Inside Child's constructor, super(...) calls Parent's constructor, and it must run before this is used — a subclass constructor has no usable this until its parent's part of the object has been built. Inside a method, super.method() calls the parent's version of that method specifically, even when the child has overridden it with its own.

class Song with constructor(title) and describe(), extended by class LiveSong extends Song, whose constructor calls super(title) first (climbs the rope) before this.venue = venue, and whose describe() calls super.describe() — super climbs up to Song's own constructor

Extend a class and call its parent's method

super(title) builds the parent portion of the object; super.describe() later calls Song's own describe, and the child's version extends that result:

javascript

s.describe() prints "Song: Blue Hour (live at The Grand)" — LiveSong's describe calls super.describe() to reuse Song's formatting, then adds its own venue text. s instanceof Song is true: an instance of a subclass is also an instance of every class it extends, all the way up.

The trap: using this before calling super()

In a subclass constructor, this isn't ready until super() has actually run — reading or writing it any earlier throws immediately:

javascript

This throws ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor. The fix: super(title) must be the first statement in a subclass constructor, before any use of this.

Quick practice

quick drill

What does class LiveSong extends Song give LiveSong access to?

quick drill

What does super(title) do inside a subclass's constructor?

quick drill

What happens if a subclass constructor uses this before calling super()?

quick drill

Inside an overridden method, what does super.describe() call?

quick drill

Given const s = new LiveSong(...) where class LiveSong extends Song, what does s instanceof Song return?

🎯 Your turn

Given class Song { constructor(title) { this.title = title; } } class LiveSong extends Song { constructor(title, venue) { this.venue = venue; super(title); } }, explain exactly why new LiveSong("a", "b") throws an error, and how to fix the constructor.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy super() must come first

Given class Song { constructor(title) { this.title = title; } } class LiveSong extends Song { constructor(title, venue) { this.venue = venue; super(title); } }, explain exactly why new LiveSong("a", "b") throws an error, and how to fix the constructor.