Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Inheritance in JavaScript

Extend a library item in JavaScript with class Book, extends, and super. Override describe to add an author; call the parent constructor before using this.

AdvancedAdvanced JavaScriptExample 2 of 12

Run this example once in a fresh browser console. The console shows the printed results.

inheritance.js
1class LibraryItem {
2 constructor(title) { this.title = title; }
3 describe() { return 'Item: ' + this.title; }
4}
5class Book extends LibraryItem {
6 constructor(title, author) { super(title); this.author = author; }
7 describe() { return super.describe() + ', author: ' + this.author; }
8}
9const book = new Book('River Walk', 'Mira Sen');
10console.log(book.describe());
11console.log('Book:', book instanceof Book);
12console.log('Library item:', book instanceof LibraryItem);

Sample Output

Item: River Walk, author: Mira Sen

The child method combines the parent description with its author.

Book: true; Library item: true

The object follows both the child and parent prototype relationships.

In this one

  • Extend a parent class
  • Initialize parent data with super
  • Override and reuse a method
  • Check a class relationship with instanceof

How it works

  1. 1

    Create LibraryItem with a title and describe method.

  2. 2

    Extend it with Book and add an author.

  3. 3

    Call super(title) before assigning this.author.

  4. 4

    Override describe while reusing the parent result.

  5. 5

    Print the full description and confirm that the book is both a Book and a LibraryItem.

Edge cases

this before super

A derived constructor cannot use this before calling super.

Let the parent constructor initialize the new object first, then add child properties.

Overriding without parent behavior

A child method replaces the looked-up parent method.

Call super.describe explicitly if you want to keep the parent description as part of the new result.

What's going on

Inheritance lets one class reuse behavior from another class. A class is a way to describe how to create objects with related data and methods. A library book is also a library item, so this example gives Book the common title behavior of LibraryItem.

extends sets up that relationship. The Book constructor calls super(title) before using this, so the parent constructor can initialize the title. It then adds an author. The child describe method overrides the parent version, which means the child supplies its own behavior for the same method name.

Inside the override, super.describe calls the parent method and adds the author to its result. instanceof shows that this object is connected to both class prototypes. Use inheritance for a clear is-a relationship, not just to avoid writing two similar lines. If a feature is merely something an object has, keeping a separate helper or contained object may be easier to follow.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What must come before this.author in this constructor?

Pick an answer to check it.

Question 2

What does overriding mean?

Pick an answer to check it.

Questions people ask

Does extends copy every method into the book?

No. The class relationship uses prototype links for method lookup.

Why is the book a LibraryItem too?

Book extends LibraryItem, so its objects follow the parent prototype relationship.

Do I always need a child constructor?

No. If the child adds no setup, JavaScript can use the default derived constructor. This one needs to store author.