How to define interfaces in Dart?
In Dart, every class defines an implicit interface. You can use an abstract class to define an interface that cannot be instantiated:
abstract class IsSilly {
void makePeopleLaugh();
}
class Clown implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}
class Comedian implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}
In Dart there is a concept of implicit interfaces.
Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.
A class implements one or more interfaces by declaring them in an
implements
clause and then providing the APIs required by the interfaces.
So your example can be translate in Dart like this :
abstract class IsSilly {
void makePeopleLaugh();
}
class Clown implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}
class Comedian implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}