Abstract methods in Typescript - Time & Space Complexity
We want to understand how using abstract methods affects the time it takes for a program to run.
Specifically, we ask: how does calling abstract methods impact the number of steps as input grows?
Analyze the time complexity of the following code snippet.
abstract class Shape {
abstract area(): number;
}
class Square extends Shape {
constructor(private side: number) { super(); }
area() { return this.side * this.side; }
}
const squares: Square[] = [new Square(2), new Square(3), new Square(4)];
for (const sq of squares) {
console.log(sq.area());
}
This code defines an abstract method area and calls it on several objects in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
areamethod on each object in the array. - How many times: Once for each object in the
squaresarray.
Each time we add one more square, we call area one more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to area |
| 100 | 100 calls to area |
| 1000 | 1000 calls to area |
Pattern observation: The number of operations grows directly with the number of objects.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of objects increases.
[X] Wrong: "Abstract methods slow down the program because they add extra steps."
[OK] Correct: Calling an abstract method is just like calling any other method; the time depends on how many times you call it, not on it being abstract.
Understanding how abstract methods affect performance helps you explain design choices clearly and confidently in interviews.
"What if the area method itself contained a loop over a large array? How would the time complexity change?"
Practice
abstract methods in TypeScript?Solution
Step 1: Understand abstract method definition
Abstract methods declare a method signature without a body inside an abstract class.Step 2: Check subclass requirements
Child classes must provide an implementation for all abstract methods.Final Answer:
They have no body and must be implemented by subclasses. -> Option AQuick Check:
Abstract methods require implementation [OK]
- Thinking abstract methods can be called directly
- Confusing abstract methods with interface methods
- Assuming abstract methods are optional to implement
Solution
Step 1: Recall abstract method syntax
Abstract methods are declared with theabstractkeyword and no body, ending with a semicolon.Step 2: Analyze options
abstract myMethod(): void;matches the correct syntax. Syntaxes that incorrectly include a body are invalid.myMethod(): void;lacks theabstractkeyword.Final Answer:
abstract myMethod(): void; -> Option BQuick Check:
Abstract method syntax = abstract myMethod(): void; [OK]
- Adding method body to abstract methods
- Omitting the abstract keyword
- Using function keyword inside class
abstract class Animal {
abstract makeSound(): string;
}
class Dog extends Animal {
makeSound(): string {
return "Woof!";
}
}
const dog = new Dog();
console.log(dog.makeSound());Solution
Step 1: Understand class hierarchy
Animal is abstract with an abstract methodmakeSound. Dog extends Animal and implementsmakeSoundreturning "Woof!".Step 2: Check instantiation and method call
Dog is instantiated (allowed), and callingmakeSound()returns "Woof!".Final Answer:
"Woof!" -> Option AQuick Check:
Implemented abstract method returns "Woof!" [OK]
- Trying to instantiate abstract class directly
- Forgetting to implement abstract method
- Expecting undefined instead of returned string
abstract class Vehicle {
abstract startEngine(): void;
}
class Car extends Vehicle {
startEngine() {
console.log("Engine started");
}
}
const myCar = new Vehicle();Solution
Step 1: Check instantiation of abstract class
The code tries to create an instance ofVehicle, which is abstract and cannot be instantiated.Step 2: Verify method implementation and class declarations
TheCarclass correctly implementsstartEngine. The method return type is optional in implementation. No need for static method or abstract Car class.Final Answer:
Cannot instantiate abstract class Vehicle. -> Option CQuick Check:
Abstract classes cannot be instantiated [OK]
- Trying to create instance of abstract class
- Thinking method must be static
- Confusing missing return type as error
abstract class Shape {
abstract area(): number;
}
// Choose the correct subclass implementation:Solution
Step 1: Check constructor and super call
class Square extends Shape { constructor(private side: number) { super(); } area(): number { return this.side * this.side; } } correctly callssuper()in constructor, required for subclass of abstract class.Step 2: Verify method signature and implementation
class Square extends Shape { constructor(private side: number) { super(); } area(): number { return this.side * this.side; } } implementsarea()returning a number, matching abstract method signature. Other implementations have errors: one missessuper()and uses an undefined variable, one returns 0 ignoring side, one returns string instead of number.Final Answer:
class Square extends Shape { constructor(private side: number) { super(); } area(): number { return this.side * this.side; } } -> Option DQuick Check:
Correct subclass calls super and matches method signature [OK]
- Omitting super() call in constructor
- Mismatching return types in method
- Using undefined variables inside method
Ready to prove it?
Pass the test and this pattern moves unknown
