Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method overriding in class inheritance
What is the output of the following JavaScript code?
Javascript
class Animal { speak() { return "Animal sound"; } } class Dog extends Animal { speak() { return "Bark"; } } const pet = new Dog(); console.log(pet.speak());
Attempts:
2 left
💡 Hint
Look at which class's speak method is called by the Dog instance.
✗ Incorrect
The Dog class overrides the speak method of Animal. So calling speak on a Dog instance returns "Bark".
❓ Predict Output
intermediate2:00remaining
Output of super keyword in subclass method
What will this code print to the console?
Javascript
class Vehicle { start() { return "Vehicle started"; } } class Car extends Vehicle { start() { return super.start() + " and car is ready"; } } const myCar = new Car(); console.log(myCar.start());
Attempts:
2 left
💡 Hint
super calls the method from the parent class.
✗ Incorrect
The Car's start method calls super.start(), which returns "Vehicle started", then adds " and car is ready".
🔧 Debug
advanced2:00remaining
Identify the error in subclass constructor
What error will this code produce when run?
Javascript
class Person { constructor(name) { this.name = name; } } class Employee extends Person { constructor(name, id) { this.id = id; super(name); } } const emp = new Employee("Alice", 123); console.log(emp.name, emp.id);
Attempts:
2 left
💡 Hint
In subclass constructors, super() must be called before using 'this'.
✗ Incorrect
JavaScript requires calling super() before accessing 'this' in subclass constructors. Here, 'this.id = id;' is before super(), causing ReferenceError.
❓ Predict Output
advanced2:00remaining
Output of static method inheritance
What will be printed by this code?
Javascript
class Parent { static greet() { return "Hello from Parent"; } } class Child extends Parent {} console.log(Child.greet());
Attempts:
2 left
💡 Hint
Static methods are inherited by subclasses.
✗ Incorrect
Static methods belong to the class itself and are inherited by subclasses unless overridden.
🧠 Conceptual
expert3:00remaining
Understanding prototype chain in class inheritance
Given the following code, what is the value of Object.getPrototypeOf(dog) === Dog.prototype and Object.getPrototypeOf(Dog.prototype) === Animal.prototype?
Javascript
class Animal {} class Dog extends Animal {} const dog = new Dog();
Attempts:
2 left
💡 Hint
Remember how JavaScript sets prototypes for instances and classes.
✗ Incorrect
dog's prototype is Dog.prototype, so Object.getPrototypeOf(dog) === Dog.prototype is true. Dog.prototype's prototype is Animal.prototype, so Object.getPrototypeOf(Dog.prototype) === Animal.prototype is true.