0
0
Typescriptprogramming~5 mins

Class implementing multiple interfaces in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean for a class to implement multiple interfaces in TypeScript?
It means the class agrees to provide all the properties and methods defined in all the interfaces it implements. This helps ensure the class meets multiple contracts or behaviors.
Click to reveal answer
beginner
How do you declare a class that implements two interfaces named <code>InterfaceA</code> and <code>InterfaceB</code>?
You write: <br><code>class MyClass implements InterfaceA, InterfaceB { /* implementation */ }</code>
Click to reveal answer
intermediate
Can a class implement interfaces that have overlapping method names? What happens?
Yes, a class can implement interfaces with overlapping method names. The class must provide one implementation that satisfies all interfaces for that method.
Click to reveal answer
intermediate
Why use multiple interfaces instead of a single big interface?
Using multiple smaller interfaces helps keep code organized and flexible. Classes can pick only the behaviors they need, making code easier to maintain and understand.
Click to reveal answer
beginner
Example: What will this code output?<br><pre>interface A { greet(): void; }<br>interface B { farewell(): void; }<br>class Person implements A, B {<br>  greet() { console.log('Hello!'); }<br>  farewell() { console.log('Goodbye!'); }<br>}<br>const p = new Person();<br>p.greet();<br>p.farewell();</pre>
The output will be:<br><pre>Hello!<br>Goodbye!</pre><br>Because the class implements both interfaces and provides both methods.
Click to reveal answer
How do you specify that a class implements multiple interfaces in TypeScript?
AList interfaces inside curly braces after the class name
BSeparate interface names with commas after the 'implements' keyword
CUse the 'extends' keyword with multiple interfaces
DUse multiple 'implements' keywords, one per interface
If two interfaces have a method with the same name but different return types, what happens when a class implements both?
AThe class can implement only one interface's method
BTypeScript allows conflicting return types without error
CThe compiler automatically merges the return types
DThe class must provide a method compatible with both interfaces
Which of these is a benefit of implementing multiple interfaces?
AAllows a class to have multiple inheritance of implementation
BAutomatically generates method implementations
CEnforces a class to follow multiple contracts or behaviors
DPrevents a class from having any methods
Can interfaces in TypeScript contain method implementations?
ANo, interfaces only declare method signatures
BYes, interfaces can have full method bodies
COnly if marked with 'default' keyword
DOnly in classes, not interfaces
What keyword is used to declare that a class follows an interface?
Aimplements
Bextends
Cuses
Dinherits
Explain how a class can implement multiple interfaces in TypeScript and why this is useful.
Think about how a class promises to have behaviors from more than one source.
You got /4 concepts.
    Describe what happens if two interfaces have methods with the same name and a class implements both.
    Consider how one method can fulfill multiple contracts.
    You got /3 concepts.