Inheritance lets one class get features from another class. It helps reuse code and organize related things together.
Inheritance using classes in Javascript
class ParentClass { // parent class code } class ChildClass extends ParentClass { // child class code }
extends keyword is used to inherit from the parent class.
The child class gets all methods and properties of the parent class automatically.
class Animal { speak() { console.log('Animal speaks'); } } class Dog extends Animal { bark() { console.log('Dog barks'); } }
class Vehicle { start() { console.log('Vehicle started'); } } class Car extends Vehicle { start() { console.log('Car started'); } }
Student inherits from Person. It uses super to call the parent constructor. Student has all Person features plus study().
class Person { constructor(name) { this.name = name; } greet() { console.log(`Hello, my name is ${this.name}`); } } class Student extends Person { constructor(name, grade) { super(name); this.grade = grade; } study() { console.log(`${this.name} is studying in grade ${this.grade}`); } } const student1 = new Student('Alice', 5); student1.greet(); student1.study();
Use super() in the child constructor to call the parent constructor.
Child classes can add new methods or override existing ones from the parent.
Inheritance helps keep code clean and easy to manage.
Inheritance lets a class get features from another class using extends.
Child classes can add or change behavior from the parent class.
Use super() to call the parent constructor inside the child.
Practice
What does the extends keyword do in JavaScript classes?
Solution
Step 1: Understand the role of
Theextendsextendskeyword is used to create a child class that inherits from a parent class.Step 2: Identify inheritance behavior
This means the child class gets all properties and methods of the parent class automatically.Final Answer:
It makes one class inherit properties and methods from another class. -> Option AQuick Check:
extendsmeans inheritance [OK]
- Confusing extends with creating instances
- Thinking extends deletes classes
- Mixing extends with private methods
Which of the following is the correct syntax to create a child class Dog that inherits from a parent class Animal?
?Solution
Step 1: Recall JavaScript inheritance syntax
JavaScript usesclass Child extends Parent {}to inherit.Step 2: Match syntax to options
Only class Dog extends Animal {} matches the correct syntax:class Dog extends Animal {}.Final Answer:
class Dog extends Animal {} -> Option AQuick Check:
Correct syntax usesextends[OK]
- Using 'inherits' instead of 'extends'
- Using ':' like other languages
- Wrong order of keywords
What will be the output of this code?
class Animal {
speak() {
return "Animal sound";
}
}
class Dog extends Animal {
speak() {
return "Bark";
}
}
const pet = new Dog();
console.log(pet.speak());Solution
Step 1: Understand method overriding in child class
TheDogclass overrides thespeakmethod fromAnimalto return "Bark".Step 2: Check which method is called
Whenpet.speak()is called, it uses theDogversion, so it returns "Bark".Final Answer:
"Bark" -> Option CQuick Check:
Child method overrides parent method [OK]
- Expecting parent method output
- Thinking both methods run
- Confusing method names
Find the error in this code and choose the fix:
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name) {
this.name = name;
}
}Solution
Step 1: Identify constructor error in child class
In JavaScript, a child class constructor must callsuper()before usingthis.Step 2: Fix by calling
Addingsuper(name);super(name);as the first line in Dog's constructor calls the parent constructor properly.Final Answer:
Addsuper(name);as the first line in Dog's constructor. -> Option BQuick Check:
Callsuper()beforethisin child constructor [OK]
- Using this before super()
- Forgetting to call super()
- Trying to access super.name directly
Given these classes, what will console.log(cat.describe()); print?
class Animal {
constructor(name) {
this.name = name;
}
describe() {
return `This is an animal named ${this.name}`;
}
}
class Cat extends Animal {
constructor(name, color) {
super(name);
this.color = color;
}
describe() {
return `${super.describe()} and its color is ${this.color}`;
}
}
const cat = new Cat("Whiskers", "black");
console.log(cat.describe());Solution
Step 1: Understand super method call in child
TheCatclass callssuper.describe()to get the parent description.Step 2: Combine parent and child descriptions
The child adds its color info, so the full string is "This is an animal named Whiskers and its color is black".Final Answer:
"This is an animal named Whiskers and its color is black" -> Option DQuick Check:
super.method() adds parent info in child method [OK]
- Ignoring super.describe() call
- Expecting only child or parent text
- Thinking super.describe is undefined
Ready to prove it?
Pass the test and this pattern moves unknown
