CodeZettaInterview Hub

JavaScript · OOP · Junior · Concept

Explain `extends` and `super` in JavaScript classes. What must a derived constructor do before using `this`?

Short Interview Answer

`extends` sets up prototype linkage; derived constructors must call `super()` before accessing `this` so the parent can initialize the instance.

Detailed Explanation

`class Child extends Parent` makes `Child.prototype` delegate to `Parent.prototype` and sets `Child.[[Prototype]]` to `Parent` for static inheritance. In the child constructor, `super(args)` invokes the parent constructor with the current `new.target`. Until `super` returns, `this` is uninitialized in the derived constructor — accessing it throws. `super.method()` in a method calls the parent prototype's method with the same `this`. Arrow fields are not on the prototype, so `super` patterns differ. Returning an object from a constructor still overrides the instance.

Example

class Animal {
  constructor(name) {
    this.name = name;
  }
}
class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}

Interview Tip

Mention static inheritance via `extends` — often overlooked.

Common Mistake

Using `this` before `super()` in a subclass constructor.

Did you know this answer?