Method overriding with types in Typescript - Time & Space Complexity
We want to understand how the time it takes to run code changes when using method overriding with types in TypeScript.
Specifically, we ask: how does the program's work grow as input or calls increase?
Analyze the time complexity of the following code snippet.
class Animal {
speak(): string {
return "...";
}
}
class Dog extends Animal {
speak(): string {
return "Woof!";
}
}
function makeSpeak(animal: Animal) {
return animal.speak();
}
const dog = new Dog();
makeSpeak(dog);
This code shows a base class with a method, a subclass that overrides it, and a function calling the method on an object.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
speakmethod on an object. - How many times: Once per call to
makeSpeak. No loops or recursion here.
Since there is no loop or repeated calls inside the snippet, the work grows directly with how many times makeSpeak is called.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls |
| 100 | 100 method calls |
| 1000 | 1000 method calls |
Pattern observation: The time grows linearly with the number of calls.
Time Complexity: O(n)
This means if you call the method n times, the total work grows roughly in direct proportion to n.
[X] Wrong: "Overriding a method makes the program slower by adding hidden loops or extra work."
[OK] Correct: Overriding just changes which code runs when the method is called. It does not add loops or repeated work by itself.
Understanding how method calls and overriding affect performance helps you explain code behavior clearly and confidently in interviews.
"What if the speak method called itself recursively? How would the time complexity change?"
Practice
What does method overriding mean in TypeScript?
Solution
Step 1: Understand method overriding
Method overriding means a child class replaces a method defined in its parent class with its own version.Step 2: Identify correct description
A child class provides its own version of a method from the parent class. correctly describes this behavior as the child class providing its own method version.Final Answer:
A child class provides its own version of a method from the parent class. -> Option AQuick Check:
Method overriding = child method replaces parent method [OK]
- Thinking parent changes child methods
- Confusing overriding with deleting methods
- Believing overriding means calling method without arguments
Which of the following is the correct syntax for overriding a method greet() in a child class in TypeScript?
class Parent {
greet(): string {
return "Hello";
}
}
class Child extends Parent {
?
}Solution
Step 1: Check method signature compatibility
The child method must have the same name and compatible return type as the parent method. Parent returns string and takes no parameters.Step 2: Identify correct override
greet(): string { return "Hi"; } matches the parent's method signature exactly (no parameters, returns string). Others differ in return type or parameters.Final Answer:
greet(): string { return "Hi"; } -> Option DQuick Check:
Override method signature must match parent [OK]
- Changing return type to number
- Adding parameters not in parent method
- Omitting return type causing implicit any
What will be the output of this TypeScript code?
class Animal {
speak(): string {
return "Animal sound";
}
}
class Dog extends Animal {
speak(): string {
return "Bark";
}
}
const pet: Animal = new Dog();
console.log(pet.speak());Solution
Step 1: Understand polymorphism with method overriding
Even though the variable type is Animal, the actual object is Dog. The overridden method in Dog is called.Step 2: Determine method output
Dog's speak() returns "Bark", so console.log prints "Bark".Final Answer:
"Bark" -> Option AQuick Check:
Overridden method runs on actual object type [OK]
- Thinking parent method runs due to variable type
- Expecting undefined because of type mismatch
- Assuming runtime error for overridden method
Identify the error in this TypeScript code that tries to override a method:
class Vehicle {
move(distance: number): void {
console.log(`Moved ${distance} meters`);
}
}
class Car extends Vehicle {
move(distance: string): void {
console.log(`Car moved ${distance}`);
}
}Solution
Step 1: Check method signature compatibility
Parent method move() takes a number parameter. Child method move() takes a string parameter, which is incompatible.Step 2: Identify correct error cause
Parameter type in child method must match parent method's parameter type. correctly states this.Final Answer:
Parameter type in child method must match parent method's parameter type. -> Option CQuick Check:
Parameter types must be compatible in overriding [OK]
- Changing parameter types in child method
- Thinking return type void is invalid
- Believing method names must differ
Given these classes, which override is valid and why?
class Base {
calculate(value: number): number {
return value * 2;
}
}
class Derived extends Base {
calculate(value: number): number | string {
if (value > 10) return "Too large";
return value * 3;
}
}Solution
Step 1: Check return type compatibility in overriding
Parent method returns number only. Child method returns number or string, which is not compatible because string is not assignable to number.Step 2: Understand TypeScript's strict method overriding rules
TypeScript requires the child method's return type to be assignable to the parent's return type, not broader. Invalid because return type must exactly match parent's return type. correctly states this.Final Answer:
Invalid because return type must exactly match parent's return type. -> Option BQuick Check:
Return type must be compatible, not broader [OK]
- Allowing broader return types in child method
- Ignoring method name consistency
- Assuming any return type is allowed in override
Ready to prove it?
Pass the test and this pattern moves unknown
