Inheritance allows a class (child) to use properties and methods from another class (parent), helping to reuse code and create relationships between classes.
Click to reveal answer
beginner
How do you create a child class that inherits from a parent class in JavaScript?
Use the <code>extends</code> keyword in the child class declaration, like <code>class Child extends Parent {}</code>.
Click to reveal answer
intermediate
What is the purpose of the <code>super()</code> function in a child class constructor?
<code>super()</code> calls the parent class constructor. It must be called before using <code>this</code> in the child constructor to properly initialize the parent part.
Click to reveal answer
intermediate
Can a child class override a method from its parent class? How?
Yes, by defining a method with the same name in the child class, it replaces the parent's method when called on child instances.
Click to reveal answer
beginner
What happens if you call a method on a child class instance that is not defined in the child but exists in the parent?
The method from the parent class is used, showing how inheritance allows child classes to access parent methods if not overridden.
Click to reveal answer
Which keyword is used to make a class inherit from another class in JavaScript?
Aextends
Binherits
Cimplements
Dsuper
✗ Incorrect
The extends keyword is used to create a child class that inherits from a parent class.
What must you call inside a child class constructor before using this?
Athis()
Bparent()
Csuper()
Dconstructor()
✗ Incorrect
super() calls the parent constructor and must be called before accessing this in the child constructor.
If a child class has a method with the same name as the parent, which method runs when called on the child?
AParent's method
BError occurs
CBoth methods run
DChild's method
✗ Incorrect
The child's method overrides the parent's method and runs instead.
What happens if a method is called on a child instance but only exists in the parent?
AError: method not found
BParent's method runs
CChild's method runs
DNothing happens
✗ Incorrect
The method from the parent class runs because the child inherits it.
Which of these is NOT true about inheritance in JavaScript classes?
AChild classes must redefine all parent methods
BChild classes can override parent methods
CChild classes can reuse parent methods
DChild classes use <code>extends</code> keyword
✗ Incorrect
Child classes do NOT need to redefine all parent methods; they inherit them automatically.
Explain how inheritance works in JavaScript classes and why it is useful.
Think about how a child class can get features from a parent class.
You got /5 concepts.
Describe the role of the super() function in a child class constructor.
Consider what happens when you create a child object that needs parent setup.
You got /3 concepts.
Practice
(1/5)
1.
What does the extends keyword do in JavaScript classes?
easy
A. It makes one class inherit properties and methods from another class.
B. It creates a new instance of a class.
C. It defines a private method inside a class.
D. It deletes a class from memory.
Solution
Step 1: Understand the role of extends
The extends keyword 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 A
Quick Check:
extends means inheritance [OK]
Hint: Remember: extends means 'inherits from' [OK]
Common Mistakes:
Confusing extends with creating instances
Thinking extends deletes classes
Mixing extends with private methods
2.
Which of the following is the correct syntax to create a child class Dog that inherits from a parent class Animal?
?
easy
A. class Dog extends Animal {}
B. class Dog inherits Animal {}
C. class Dog : Animal {}
D. class Dog Animal extends {}
Solution
Step 1: Recall JavaScript inheritance syntax
JavaScript uses class 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 A
Quick Check:
Correct syntax uses extends [OK]
Hint: Use 'extends' keyword to inherit classes [OK]
Common Mistakes:
Using 'inherits' instead of 'extends'
Using ':' like other languages
Wrong order of keywords
3.
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());
medium
A. undefined
B. "Animal sound"
C. "Bark"
D. Error: speak is not a function
Solution
Step 1: Understand method overriding in child class
The Dog class overrides the speak method from Animal to return "Bark".
Step 2: Check which method is called
When pet.speak() is called, it uses the Dog version, so it returns "Bark".
Final Answer:
"Bark" -> Option C
Quick Check:
Child method overrides parent method [OK]
Hint: Child method replaces parent method if same name [OK]
Common Mistakes:
Expecting parent method output
Thinking both methods run
Confusing method names
4.
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;
}
}
medium
A. Add extends Animal to Dog class.
B. Add super(name); as the first line in Dog's constructor.
C. Change this.name to super.name in Dog's constructor.
D. Add super(name); as the last line in Dog's constructor.
Solution
Step 1: Identify constructor error in child class
In JavaScript, a child class constructor must call super() before using this.
Step 2: Fix by calling super(name);
Adding super(name); as the first line in Dog's constructor calls the parent constructor properly.
Final Answer:
Add super(name); as the first line in Dog's constructor. -> Option B
Quick Check:
Call super() before this in child constructor [OK]
Hint: Always call super() first in child constructor [OK]
Common Mistakes:
Using this before super()
Forgetting to call super()
Trying to access super.name directly
5.
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());
hard
A. "This is an animal named Whiskers"
B. Error: super.describe is not a function
C. "This is a black animal named Whiskers"
D. "This is an animal named Whiskers and its color is black"
Solution
Step 1: Understand super method call in child
The Cat class calls super.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 D
Quick Check:
super.method() adds parent info in child method [OK]
Hint: Use super.method() to extend parent method output [OK]
Common Mistakes:
Ignoring super.describe() call
Expecting only child or parent text
Thinking super.describe is undefined
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.