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?"
Practice
What does it mean when a TypeScript class implements multiple interfaces?
Choose the best explanation.
Solution
Step 1: Understand what interfaces define
Interfaces describe method and property shapes but do not provide code.Step 2: Understand what implementing multiple interfaces means
A class implementing multiple interfaces must provide all methods and properties from each interface.Final Answer:
The class promises to have all methods and properties from all those interfaces. -> Option BQuick Check:
Implementing multiple interfaces means fulfilling all their contracts [OK]
- Thinking interfaces provide code to inherit
- Believing a class can only implement one interface
- Confusing interfaces with classes
Which of the following is the correct syntax for a class Car implementing two interfaces Engine and Wheels?
Solution
Step 1: Recall TypeScript syntax for multiple interfaces
Useimplements Interface1, Interface2to implement multiple interfaces.Step 2: Check each option
class Car implements Engine, Wheels {} uses correct syntax. The options using extends and inherits use invalid keywords. class Car implements (Engine & Wheels) {} uses invalid parentheses and &.Final Answer:
class Car implements Engine, Wheels {} -> Option AQuick Check:
Multiple interfaces use comma-separated list after implements [OK]
- Using extends instead of implements for interfaces
- Trying to use & or parentheses in implements clause
- Using inherits keyword which doesn't exist
Consider these interfaces and class:
interface A { foo(): string; }
interface B { bar(): number; }
class C implements A, B {
foo() { return "hello"; }
bar() { return 42; }
}
const c = new C();
console.log(c.foo(), c.bar());What is the output?
Solution
Step 1: Understand method return values
Methodfoo()returns string "hello" andbar()returns number 42.Step 2: Understand console.log output
console.log prints values separated by space without quotes for strings.Final Answer:
hello 42 -> Option AQuick Check:
Method returns printed as values without quotes [OK]
- Expecting quotes around string output
- Confusing object output with string
- Thinking code causes syntax error
What is wrong with this TypeScript code?
interface X { doX(): void; }
interface Y { doY(): void; }
class Z implements X, Y {
doX() { console.log("X done"); }
}Solution
Step 1: Check all interface methods
Interface Y requires methoddoY()which is missing in class Z.Step 2: Understand TypeScript rules for implementing interfaces
A class must implement all methods from all interfaces it declares.Final Answer:
Class Z must implement method doY() from interface Y. -> Option CQuick Check:
All interface methods must be implemented [OK]
- Thinking partial implementation is allowed
- Confusing interfaces with classes
- Believing interfaces provide code
You have these interfaces:
interface Printer { print(): void; }
interface Scanner { scan(): string; }Write a class AllInOne that implements both interfaces and stores the last scanned text in a property lastScan. Which code snippet correctly implements this?
Solution
Step 1: Check interface methods implemented
Printer requiresprint(), Scanner requiresscan(). class AllInOne implements Printer, Scanner { lastScan = ""; print() { console.log("Printing..."); } scan() { this.lastScan = "document"; return this.lastScan; } } implements both.Step 2: Check lastScan property usage
class AllInOne implements Printer, Scanner { lastScan = ""; print() { console.log("Printing..."); } scan() { this.lastScan = "document"; return this.lastScan; } } initializeslastScanand updates it insidescan(), fulfilling the requirement.Final Answer:
class AllInOne implements Printer, Scanner { lastScan = ""; print() { console.log("Printing..."); } scan() { this.lastScan = "document"; return this.lastScan; } } -> Option DQuick Check:
Implement all methods and update lastScan property [OK]
- Omitting one interface method
- Not updating lastScan property
- Declaring property but not initializing or updating it
Ready to prove it?
Pass the test and this pattern moves unknown
