Interfaces

Dart does not have the interface keyword but does allow us to use interfaces in a subtly different way from what you may be used to. All class declarations are themselves interfaces. This means that, when you are defining a class in Dart, you are also defining an interface that may be implemented and not only extended by other classes. This is called implicit interfaces in the Dart world.

On this basis, our previous Person class is also a Person interface that could be implemented, instead of extended, by the Student class:

class Student implements Person {
String nickName;

@override
String firstName;

@override
String lastName;

Student(this.firstName, this.lastName, this.nickName);

@override
String get fullName => "$firstName $lastName";

@override
String toString() => "$fullName, also known as $nickName";
}

Note that, in general, the code does not change too much, except inasmuch as the members are now defined in the Student class. The Person class is just a contract that the Student class adopted and must implement.

If you want to declare an explicit interface, you just need to make an abstract class without any implementation on it, just member definitions, and it will be a pure interface, ready to be implemented.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.171.121