Class implementing multiple interfaces in Typescript - Time & Space Complexity
When a class implements multiple interfaces, it must provide all the methods those interfaces require.
We want to see how the time to create and use such a class grows as we add more interfaces or methods.
Analyze the time complexity of the following code snippet.
interface A {
methodA(): void;
}
interface B {
methodB(): void;
}
class Multi implements A, B {
methodA() { console.log('A'); }
methodB() { console.log('B'); }
}
const obj = new Multi();
obj.methodA();
obj.methodB();
This code defines two interfaces and a class that implements both, then calls their methods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling each method once.
- How many times: Each method runs exactly once per call.
Each method runs independently, so adding more methods means more calls.
| Input Size (number of methods) | Approx. Operations |
|---|---|
| 2 | 2 method calls |
| 10 | 10 method calls |
| 100 | 100 method calls |
Pattern observation: The total work grows directly with the number of methods called.
Time Complexity: O(n)
This means the time grows in a straight line with the number of methods you call on the class.
[X] Wrong: "Implementing more interfaces makes the program slower in a complex way."
[OK] Correct: Each method runs independently, so adding interfaces just adds more simple calls, not complicated slowdowns.
Understanding how multiple interfaces affect method calls helps you explain design choices clearly and shows you know how code scales.
"What if each method called inside the class runs a loop over an array? How would the time complexity change?"