What if you could give one class many clear jobs without mixing things up?
Why Class implementing multiple interfaces in Typescript? - Purpose & Use Cases
Imagine you have a robot that needs to do many jobs: it can clean, cook, and also entertain guests. If you try to write separate instructions for each job manually every time you build a new robot, it becomes confusing and hard to keep track.
Writing all the instructions for each robot from scratch is slow and easy to mess up. You might forget some steps or mix up the order. It's like trying to remember every detail without a checklist, which leads to mistakes and wasted time.
Using a class that implements multiple interfaces is like giving the robot a clear set of checklists for each job it can do. The class promises to follow all these checklists, so you know exactly what the robot can do and how. This keeps things organized and easy to manage.
class Robot {
clean() { /* cleaning code */ }
cook() { /* cooking code */ }
entertain() { /* entertaining code */ }
}interface Cleaner { clean(): void; }
interface Cook { cook(): void; }
interface Entertainer { entertain(): void; }
class Robot implements Cleaner, Cook, Entertainer {
clean() { /* cleaning code */ }
cook() { /* cooking code */ }
entertain() { /* entertaining code */ }
}This lets you build clear, organized programs where one class can promise to do many different jobs, making your code easier to understand and reuse.
Think of a smartphone app that can send messages, play music, and track fitness. Each feature is like an interface, and the app class implements all of them to work smoothly together.
Manual coding for multiple roles is confusing and error-prone.
Implementing multiple interfaces organizes responsibilities clearly.
It makes your code easier to read, maintain, and expand.
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
