Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Calling Parent Constructors with super | Inheritance and Encapsulation
JavaScript Classes and OOP Foundations

bookCalling Parent Constructors with super

When you create a subclass in JavaScript using the extends keyword, the subclass inherits properties and methods from its parent class. However, to properly initialize the parent class's properties within the subclass's constructor, you must use the super keyword. The super keyword acts as a reference to the parent class's constructor and allows you to call it from within the subclass. This is essential because, in JavaScript, a subclass constructor cannot access this until after it has called super. By calling super, you ensure that all properties defined in the parent class are correctly set up before you add or modify properties specific to the subclass. This mechanism not only enables property inheritance but also allows you to extend or customize the initialization process for your subclasses.

1234567891011121314151617181920
// Parent class class Vehicle { constructor(make, model) { this.make = make; this.model = model; } } // Subclass class Car extends Vehicle { constructor(make, model, doors) { super(make, model); // Call parent constructor this.doors = doors; // Additional property for Car } } const myCar = new Car("Toyota", "Corolla", 4); console.log(myCar.make); // Output: Toyota console.log(myCar.model); // Output: Corolla console.log(myCar.doors); // Output: 4
copy
question mark

Which statements about calling the parent constructor with super in JavaScript subclasses are correct?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what happens if you forget to call `super` in the subclass constructor?

How does method overriding work in JavaScript subclasses?

Can you show an example with more than one level of inheritance?

Awesome!

Completion rate improved to 6.25

bookCalling Parent Constructors with super

Swipe to show menu

When you create a subclass in JavaScript using the extends keyword, the subclass inherits properties and methods from its parent class. However, to properly initialize the parent class's properties within the subclass's constructor, you must use the super keyword. The super keyword acts as a reference to the parent class's constructor and allows you to call it from within the subclass. This is essential because, in JavaScript, a subclass constructor cannot access this until after it has called super. By calling super, you ensure that all properties defined in the parent class are correctly set up before you add or modify properties specific to the subclass. This mechanism not only enables property inheritance but also allows you to extend or customize the initialization process for your subclasses.

1234567891011121314151617181920
// Parent class class Vehicle { constructor(make, model) { this.make = make; this.model = model; } } // Subclass class Car extends Vehicle { constructor(make, model, doors) { super(make, model); // Call parent constructor this.doors = doors; // Additional property for Car } } const myCar = new Car("Toyota", "Corolla", 4); console.log(myCar.make); // Output: Toyota console.log(myCar.model); // Output: Corolla console.log(myCar.doors); // Output: 4
copy
question mark

Which statements about calling the parent constructor with super in JavaScript subclasses are correct?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt