I can't get a clue why do we need implements? According to this https://dart.dev/language/classes#implicit-interfaces After we used implements we should rewrite everything we have in parent class except constructor.
// A person. The implicit interface contains greet().
class Person {
// In the interface, but visible only in this library.
final String _name;
// Not in the interface, since this is a constructor.
Person(this._name);
// In the interface.
String greet(String who) => 'Hello, $who. I am $_name.';
}
// An implementation of the Person interface.
class Impostor implements Person {
String get _name => '';
String greet(String who) => 'Hi $who. Do you know who I am?';
}
So my question actually is why can't we just create a new class instead of use implements?
The point of using
Derived implements Baseis to specify thatDerivedconforms to the same interface asBase.Derivedis substitutable wherever aBaseobject is expected. If you instead created a new, unrelated class, then the type system would prevent you from passing an instance of that class as aBase. (In languages that do not have static typing, you would not need something likeimplementssince you instead could use duck typing. If you really wanted, you could do that in Dart too if you useddynamic.)In contrast to
extends,implementsallows a class to provide multiple unrelated interfaces without the ambiguity that can come from true multiple inheritance.