Course contentsShow
← JavaScript
Lesson 1825 of 2,72338. Object-Oriented PatternsFree lesson

When to Still Use Inheritance

Recognize scenarios where single-level inheritance remains simpler than composition, such as extending built-ins.

When to Still Use Inheritance

You'll learn when single-level inheritance is actually simpler than composition patterns.

The Composition-First Mindset... With Exceptions

We've spent considerable time understanding why composition often beats inheritance. But inheritance isn't always the wrong choice. For certain scenarios—especially shallow, single-level hierarchies—inheritance remains the clearest, most maintainable solution.

Where Inheritance Still Shines

Extending Built-ins

When you need to add custom behavior to built-in JavaScript types like Array, Error, or Map, inheritance is the natural approach:

class ValidationError extends Error {
  constructor(message, field) {
    super(message);
    this.name = 'ValidationError';
    this.field = field;
  }
}

throw new ValidationError('Invalid email', 'email');
// Works perfectly with try/catch, instanceof, stack traces

Trying to compose an Error object would lose critical features like stack traces and proper instanceof checks.

Simple "Is-A" Relationships

When you have a genuinely simple, single-level specialization:

class Button {
  constructor(label) {
    this.label = label;
  }
  render() { return `<button>${this.label}</button>`; }
}

class IconButton extends Button {
  constructor(label, icon) {
    super(label);
    this.icon = icon;
  }
  render() { return `<button>${this.icon} ${this.label}</button>`; }
}

This is straightforward and readable. Composition would add complexity without benefit here.

The Guideline

Use inheritance for one level deep when you're genuinely extending a concept (especially built-ins). The moment you need a second or third level, or multiple behavior mixtures, switch to composition.

Key Takeaway: Inheritance isn't evil—it's the depth and rigidity of inheritance hierarchies that cause problems. Single-level extensions of built-ins or simple specializations are perfectly appropriate uses of extends.