Interfaces and Classes
Intro to Interfaces and Classes
Interfaces define contracts that classes can implement. They specify what methods
and properties a class must have, without providing the implementation.
interface Flyable {
fly(): void
}
class Bird implements Flyable {
fly(): void {
console.log('Flying!')
}
}
Implementing Interfaces
When a class implements an interface, it must provide all required members:
interface Drawable {
draw(): void
}
class Circle implements Drawable {
draw(): void {
console.log('Drawing a circle')
}
}
Multiple Interfaces
A class can implement multiple interfaces:
interface Swimmable {
swim(): void
}
interface Flyable {
fly(): void
}
class Duck implements Swimmable, Flyable {
swim(): void {
console.log('Swimming')
}
fly(): void {
console.log('Flying')
}
}
Programming to Abstractions
Use interfaces as types to write flexible code:
function processAnimal(animal: Flyable) {
animal.fly() // Works with any class that implements Flyable
}
processAnimal(new Bird())
processAnimal(new Duck())
Interfaces enable polymorphismβyou can treat different classes the same way if
they implement the same interface.
In this exercise, you'll implement interfaces and program to abstractions.