🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #218

JS Class Inheritance


Class Inheritance

To create a class inheritance, use the extends keyword.

A class created with a class inheritance inherits all the methods from another class:

Example

class Car {
  constructor(brand) {
    this.carname =
  brand;

  }
  present() {
    return 'I have a ' + this.carname;

  }
}

class Model extends Car {

  constructor(brand, mod) {
    super(brand);
    this.model = mod;

  }
  show() {

      return this.present() + ', it is a ' + this.model;

  }
}

let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML
  = myCar.show();

The super() method refers to the parent class.

By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods.

Note: Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.


Getters and Setters

Classes also allow you to use getters and setters.

It can be smart to use getters and setters for your properties, especially if you want to do something special with the value before returning them, or before you set them.

To add getters and setters in the class, use the get and set keywords.

Example

  class Car {
  constructor(brand) {
    this.carname
  = brand;
  }
  get cnam() {

  return this.carname;
  }
  set cnam(x) {

  this.carname = x;
  }
}

const myCar = new Car("Ford");

  document.getElementById("demo").innerHTML = myCar.cnam;

Note: even if the getter is a method, you do not use parentheses when you want to get the property value.

The name of the getter/setter method cannot be the same as the name of the property, in this case carname.

Many programmers use an underscore character _ before the property name to separate the getter/setter from the actual property:

Example

  class Car {
  constructor(brand) {
    this._carname
  = brand;
  }
  get carname() {

  return this._carname;
  }
  set carname(x) {

  this._carname = x;
  }
}

const myCar = new Car("Ford");

  document.getElementById("demo").innerHTML = myCar.carname;

To use a setter, use the same syntax as when you set a property value, without parentheses:

Example

  class Car {
  constructor(brand) {
    this._carname
  = brand;
  }
  get carname() {

  return this._carname;
  }
  set carname(x) {

  this._carname = x;
  }
}

const myCar = new Car("Ford");

  myCar.carname = "Volvo";

  document.getElementById("demo").innerHTML = myCar.carname;

Hoisting

Unlike functions, and other JavaScript declarations, class declarations are not hoisted.

That means that you must declare a class before you can use it:

Example

  //You cannot use the class yet.
//myCar = new Car("Ford") will raise an error.

class Car {

  constructor(brand) {
    this.carname = brand;

  }
}

//Now you can use the class:
const myCar = new Car("Ford")

Note: For other declarations, like functions, you will NOT get an error when you try to use it before it is declared, because the default behavior of JavaScript declarations are hoisting (moving the declaration to the top).

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS Class Inheritance – FAQs

Quick answers about learning JS Class Inheritance in JavaScript.

This free note from CodingNow 2.0 explains JS Class Inheritance in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS Class Inheritance, is 100% free with no signup required.
With focused practice, most students grasp JS Class Inheritance in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now