0
0
Typescriptprogramming~5 mins

Implementing interfaces in classes in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in TypeScript?
An interface in TypeScript defines a contract or a shape for objects. It specifies what properties and methods an object should have, without providing the implementation.
Click to reveal answer
beginner
How do you make a class follow an interface in TypeScript?
You use the <code>implements</code> keyword after the class name, followed by the interface name. This means the class must provide all properties and methods defined in the interface.
Click to reveal answer
intermediate
What happens if a class does not implement all properties or methods of an interface it claims to implement?
TypeScript will show an error because the class does not fulfill the contract of the interface. The class must implement all required members.
Click to reveal answer
intermediate
Can a class implement multiple interfaces in TypeScript?
Yes, a class can implement multiple interfaces by listing them separated by commas after the <code>implements</code> keyword.
Click to reveal answer
beginner
Example: What does this code do?<br><pre>interface Animal {<br>  name: string;<br>  makeSound(): void;<br>}<br><br>class Dog implements Animal {<br>  name: string;<br>  constructor(name: string) {<br>    this.name = name;<br>  }<br>  makeSound() {<br>    console.log('Woof!');<br>  }<br>}</pre>
This code defines an interface Animal with a property name and a method makeSound. The class Dog implements this interface by providing the name property and the makeSound method that prints 'Woof!'.
Click to reveal answer
Which keyword is used in TypeScript for a class to follow an interface?
Aextends
Bimplements
Cinherits
Duses
What will happen if a class misses a method defined in its interface?
AThe interface will be ignored
BThe class will work fine
CThe method will be added automatically
DTypeScript will show an error
Can a class implement more than one interface?
AYes, by listing interfaces separated by commas
BNo, only one interface is allowed
CYes, but only if interfaces extend each other
DNo, classes cannot implement interfaces
What does an interface define?
AThe shape of an object (properties and methods)
BThe implementation of methods
CThe variables inside a class
DThe inheritance chain
In this code snippet, what is the role of makeSound()?<br>
interface Animal {<br>  makeSound(): void;<br>}<br>class Cat implements Animal {<br>  makeSound() { console.log('Meow'); }<br>}
AIt is a variable
BIt is a property of the interface
CIt is a method the class must implement
DIt is a constructor
Explain how a class implements an interface in TypeScript and why it is useful.
Think about how interfaces act like a promise for the class.
You got /4 concepts.
    Describe what happens if a class does not fully implement an interface it claims to implement.
    What does TypeScript do to keep your code correct?
    You got /4 concepts.