What if you could change just one action in your program without rebuilding everything from scratch?
Why Method overriding in Javascript? - Purpose & Use Cases
Imagine you have a basic toy robot that can say "Hello". Now, you want a new robot that says "Hello" but also adds "I am your helper." If you had to build a whole new robot from scratch just to change this one behavior, it would take a lot of time and effort.
Manually rewriting or copying all the robot's features just to change one small behavior is slow and error-prone. You might forget some features or introduce bugs. It's like rewriting the whole recipe just to add a pinch of salt.
Method overriding lets you keep the original robot's features but change just the part you want. You create a new robot that uses the old one's abilities but replaces the "sayHello" action with your own version. This saves time and keeps your code clean.
class Robot { sayHello() { console.log('Hello'); } } class NewRobot { sayHello() { console.log('Hello'); console.log('I am your helper.'); } // Need to rewrite all other methods too }
class Robot { sayHello() { console.log('Hello'); } } class NewRobot extends Robot { sayHello() { super.sayHello(); console.log('I am your helper.'); } }
It enables you to customize or improve specific behaviors in your programs without rewriting everything, making your code smarter and easier to maintain.
Think of a video game where the basic character can walk and jump. A new character might walk and jump differently or add new moves. Method overriding lets the new character keep the basic moves but change how they work.
Method overriding lets you change specific behaviors in inherited code.
It avoids rewriting entire classes, saving time and reducing errors.
It helps create flexible and maintainable programs.
Practice
Solution
Step 1: Recall method overriding concept
Method overriding means a child class uses the same method name as the parent to change its behavior.Step 2: Identify what overriding does
It replaces the parent's method with a new version in the child class.Final Answer:
Replace a method from the parent class with a new version -> Option AQuick Check:
Method overriding = Replace parent method [OK]
- Thinking overriding creates a new method with a different name
- Believing overriding deletes parent methods
- Confusing overriding with method calling
greet() in a child class Dog that extends Animal?Solution
Step 1: Check method syntax in class
In JavaScript classes, methods are defined without keywords like function or var inside the class body.Step 2: Identify correct method override syntax
class Dog extends Animal { greet() { console.log('Woof!'); } } correctly definesgreet()as a method inside the class body.Final Answer:
class Dog extends Animal { greet() { console.log('Woof!'); } } -> Option CQuick Check:
Method override syntax = methodName() { } [OK]
- Using function keyword inside class method
- Using var or let to define methods inside class
- Using arrow functions incorrectly for overriding
class Animal {
speak() { return 'Animal sound'; }
}
class Cat extends Animal {
speak() { return 'Meow'; }
}
const pet = new Cat();
console.log(pet.speak());Solution
Step 1: Identify method overriding in Cat class
Thespeak()method inCatreplaces the one inAnimal.Step 2: Check which method is called on
Sincepetpetis an instance ofCat,Cat'sspeak()runs, returning 'Meow'.Final Answer:
'Meow' -> Option AQuick Check:
Child method runs = 'Meow' [OK]
- Expecting parent method output instead of child
- Confusing instance type with method called
- Assuming undefined or error due to override
run() method:class Vehicle {
run() {
console.log('Vehicle running');
}
}
class Car extends Vehicle {
run = () => {
console.log('Car running fast');
}
}Solution
Step 1: Check method definition style in class
Class methods should be defined using method syntax, not arrow functions assigned to properties.Step 2: Identify why arrow function causes error
Usingrun = () => {}creates a class field, not a proper method, so it does not override the parent method correctly.Final Answer:
Arrow function syntax is invalid for method overriding in class -> Option BQuick Check:
Override needs method syntax, not arrow function [OK]
- Using arrow functions inside class for overriding
- Forgetting method syntax rules
- Thinking super call is mandatory for override
Shape with a method area() that returns 0. You want to create a child class Rectangle that overrides area() to return width x height. Which code correctly implements this?Solution
Step 1: Check constructor and property setup
class Rectangle extends Shape { constructor(w, h) { super(); this.w = w; this.h = h; } area() { return this.w * this.h; } } correctly callssuper()and assigns width and height tothis.Step 2: Verify method overriding and usage of properties
Thearea()method usesthis.wandthis.hto calculate area, correctly overriding the parent method.Final Answer:
class Rectangle extends Shape { constructor(w, h) { super(); this.w = w; this.h = h; } area() { return this.w * this.h; } } -> Option DQuick Check:
Override with constructor + this properties + method [OK]
- Forgetting to call super() in constructor
- Using parameters inside area() instead of properties
- Defining area as arrow function (less common but works differently)
Ready to prove it?
Pass the test and this pattern moves unknown
