FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Objects, Prototypes & Classes

Advanced

Class Inheritance

Class inheritance lets one class extend another, but composition is often simpler when behavior does not truly form a hierarchy.

class Widget {
  constructor(title) { this.title = title; }
}

class ButtonWidget extends Widget {
  click() { return `${this.title} clicked`; }
}

Objects, Prototypes & Classes

Inheritance shares behavior through an extends relationship.

A subclass extends a parent class. The subclass inherits methods from the parent and can add or override behavior.

When a subclass has its own constructor, it must call super() before using this. super() runs the parent constructor.

Inheritance is useful for real is-a relationships. If the relationship is only about sharing a small helper, composition or plain functions are often cleaner.

extends

Creates a subclass relationship.

super()

Calls the parent constructor.

Override

A subclass can replace a parent method.

Composition

Using smaller objects/functions instead of a class hierarchy.

Examples

Object code becomes clear when shape, state and behavior are visible.

Small hierarchy with a real relationship

class Widget {
  constructor(title) {
    this.title = title;
  }
}

class Panel extends Widget {
  renderTitle() {
    return this.title;
  }
}

Inheritance just to share one helper

class FormatHelpers {
  format(value) { return value.trim(); }
}

class Profile extends FormatHelpers {}

// A plain format function would be clearer.

Code patterns

Reusable examples for quick reference.

These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.

Basic inheritance

Subclass inherits parent methods.

class Widget {
  label() { return "Widget"; }
}

class Panel extends Widget {}
console.log(new Panel().label());

super constructor

Initialize parent state first.

class Widget {
  constructor(title) { this.title = title; }
}

class Panel extends Widget {
  constructor(title, size) {
    super(title);
    this.size = size;
  }
}

Override method

Subclass can customize behavior.

class Widget { label() { return "Widget"; } }

class Panel extends Widget {
  label() { return "Panel"; }
}

Call parent method

super can call parent methods too.

class Widget { label() { return "Widget"; } }
class Panel extends Widget {
  label() { return `${super.label()} Panel`; }
}

Rules that matter

Model data deliberately before adding behavior.

Objects are where JavaScript programs start to model real application state. Good object shape makes later functions, classes and UI updates much easier to maintain.

Use inheritance for real hierarchies

A subclass should truly be a specialized parent.

Call super before this

Subclass constructors must initialize parent state first.

Keep hierarchies shallow

Deep inheritance becomes hard to debug.

Prefer composition for shared helpers

Passing functions or objects is often simpler.

Override deliberately

Changing parent behavior should be obvious.

Test parent and subclass behavior

Inherited behavior can break in subtle ways.

Production thinking

Object design decides how understandable the rest of the code can be.

Why does this matter?

Inheritance is powerful, but it creates coupling. Use it when it clarifies the model, not just to reuse a few lines of code.

Accessibility

Inherited UI components should not hide accessible behavior in a distant parent class.

Production note

Production class hierarchies should stay shallow and have clear contracts between parent and subclass.

SEO note

Inherited render methods must still produce predictable visible content and metadata.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Add another subclass with a different label.
  • Remove super(title) and read the error.
  • Rewrite the example with a plain function instead of inheritance.

Practice assignment

Do this before moving to the next topic.

  1. Create one parent class.
  2. Create one subclass with extends.
  3. Override one method and call super.

Try it yourself

Extend a widget class

Live preview

Self-check

Before you continue, prove that you understand Class Inheritance.

Advanced

Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.

  1. Can you explain extends?
  2. Can you explain super()?
  3. Can you explain method overriding?
  4. Can you explain composition versus inheritance?
  5. Can you explain why deep inheritance is risky?

Chapter checkpoint

Objects, Prototypes & Classes checkpoint

Finish this chapter by turning the lessons into a small practical proof.

Build

Build a small example that combines three lessons from this chapter.

Deliverables

  • working code
  • short explanation
  • self-check answers

Quality checks

  • readable code
  • clear failure path
  • accessibility considered

Review question

Can you explain the important tradeoff without reading from the page?