Glossary / extends and super

extends and super

extends establishes a subclass relationship between classes or constructor functions. super calls a parent constructor or references a parent method. For a derived class, super() must run before the constructor reads or writes this, because base construction initializes the instance. These features rely on linked prototype chains and preserve dynamic dispatch.

Constructor and method behavior

class Base {
  constructor(id) { this.id = id; }
  describe() { return `#${this.id}`; }
}
class Item extends Base {
  constructor(id, label) {
    super(id);
    this.label = label;
  }
  describe() { return `${super.describe()}: ${this.label}`; }
}

super.describe() resolves the parent method but calls it with the current this receiver. It is not simply a fixed parent object. Static inheritance is also linked: subclasses can inherit static methods. If a derived constructor omits its own constructor, a default constructor forwards arguments to the base.

A base constructor calling an overridable method can invoke subclass behavior before subclass fields initialize; avoid virtual calls from constructors. Overrides should preserve the base contract, and constructors should not establish partially valid states. Built-in subclassing has historical and host-specific details, so test target runtimes where extending native classes is required.

Design guidance

Use inheritance for a genuine “is-a” relationship with substitutable behavior. Use composition for optional capabilities. Keep hierarchy shallow and document protected extension points; a subclass can break invariants if base methods assume behavior not guaranteed by the contract.

Interview questions

  1. Why must super() precede this in a derived constructor? The base constructor must initialize the instance first.
  2. What is this in super.method()? The current receiver, not a new parent object.
  3. What is a constructor dispatch hazard? A base constructor calls an override before subclass state is initialized.
  4. When choose composition over extends? When reusing capability without a true subtype contract.

References