Implicit interfaces

Every class implicitly defines an interface containing all the instancemembers of the class and of any interfaces it implements. If you want tocreate a class A that supports class B’s API without inheriting B’simplementation, class A should implement the B interface.

A class implements one or more interfaces by declaring them in animplements clause and then providing the APIs required by theinterfaces. For example:

  1. // A person. The implicit interface contains greet().
  2. class Person {
  3. // In the interface, but visible only in this library.
  4. final _name;
  5. // Not in the interface, since this is a constructor.
  6. Person(this._name);
  7. // In the interface.
  8. String greet(String who) => 'Hello, $who. I am $_name.';
  9. }
  10. // An implementation of the Person interface.
  11. class Impostor implements Person {
  12. get _name => '';
  13. String greet(String who) => 'Hi $who. Do you know who I am?';
  14. }
  15. String greetBob(Person person) => person.greet('Bob');
  16. void main() {
  17. print(greetBob(Person('Kathy')));
  18. print(greetBob(Impostor()));
  19. }

Here’s an example of specifying that a class implements multipleinterfaces:

  1. class Point implements Comparable, Location {...}