Method overriding in Javascript - Time & Space Complexity
We want to understand how the time it takes to run code changes when we use method overriding in JavaScript.
Specifically, how does calling an overridden method affect the work done as the program runs?
Analyze the time complexity of the following code snippet.
class Animal {
speak() {
console.log('Animal speaks');
}
}
class Dog extends Animal {
speak() {
console.log('Dog barks');
}
}
const pet = new Dog();
pet.speak();
This code shows a class Dog overriding the speak method from its parent Animal class, then calling the overridden method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single method call to
speak(). - How many times: The method is called once in this example.
Since the method call happens once, the work done stays the same no matter how many objects or classes exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 method call |
| 10 | 10 method calls if called on 10 objects |
| 100 | 100 method calls if called on 100 objects |
Pattern observation: The time grows directly with how many times the method is called, not with the complexity of overriding itself.
Time Complexity: O(1)
This means calling an overridden method takes a fixed amount of time each call, regardless of class hierarchy size.
[X] Wrong: "Overriding a method makes the program slower as the class hierarchy grows."
[OK] Correct: The method call itself runs in constant time; the class hierarchy size does not add extra steps during the call.
Understanding that method overriding does not add hidden time costs helps you explain how object-oriented features work efficiently in real code.
"What if the overridden method calls the parent method inside it? How would the time complexity change?"
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
