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?
✗ Incorrect
In TypeScript, you list multiple interfaces separated by commas after the 'implements' keyword.
If two interfaces have a method with the same name but different return types, what happens when a class implements both?
✗ Incorrect
The class must provide a method that satisfies both interfaces, so the method must be compatible with both return types.
Which of these is a benefit of implementing multiple interfaces?
✗ Incorrect
Implementing multiple interfaces enforces that a class follows multiple contracts or behaviors.
Can interfaces in TypeScript contain method implementations?
✗ Incorrect
Interfaces only declare method signatures; they do not contain implementations.
What keyword is used to declare that a class follows an interface?
✗ Incorrect
The 'implements' keyword declares that a class follows an interface.
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.