Using multiple interfaces lets a class promise to do many different jobs. This helps organize code and makes it clear what the class can do.
Class implementing multiple interfaces in Typescript
interface Interface1 {
method1(): void;
}
interface Interface2 {
method2(): void;
}
class MyClass implements Interface1, Interface2 {
method1(): void {
// code for method1
}
method2(): void {
// code for method2
}
}You list multiple interfaces separated by commas after implements.
The class must provide all methods from all interfaces it implements.
Duck promises to fly and swim by implementing both interfaces.interface Flyer {
fly(): void;
}
interface Swimmer {
swim(): void;
}
class Duck implements Flyer, Swimmer {
fly(): void {
console.log("Duck is flying");
}
swim(): void {
console.log("Duck is swimming");
}
}Book class can both read and write text by following two interfaces.interface Reader {
read(): string;
}
interface Writer {
write(text: string): void;
}
class Book implements Reader, Writer {
private content = "";
read(): string {
return this.content;
}
write(text: string): void {
this.content += text;
}
}This program shows a class ConsoleLogger that can both log messages and handle errors by implementing two interfaces.
interface Logger {
log(message: string): void;
}
interface ErrorHandler {
handleError(error: string): void;
}
class ConsoleLogger implements Logger, ErrorHandler {
log(message: string): void {
console.log(`Log: ${message}`);
}
handleError(error: string): void {
console.error(`Error: ${error}`);
}
}
const logger = new ConsoleLogger();
logger.log("Application started");
logger.handleError("File not found");Interfaces only describe what methods a class should have, not how they work.
A class can implement any number of interfaces to combine features.
If you forget to add a method from an interface, TypeScript will show an error.
Classes can promise to do many things by implementing multiple interfaces.
This helps keep code organized and clear.
Always implement all methods from every interface you list.
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
