Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 2: Learn Inheritance

16. Implement an Interface

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 15. Use Abstract Classes Next episode: 17. Define a Generic Class

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Interfaces are contracts. They detail what an object can do. When we create an object, we are defining such an interface. Most of the time, that interface is for internal use only. That is, we have no intention for other objects to use it. But there are times when we want other objects to use it.

class Item {
  int id;
  Item(this.id);
  
  @override
  String toString() => 'item ${id.toString()}';
}
class Repository {
  
  var items = <Item>[];
  
  void update(Item item) {
    print('update: $item');
  }
  
  void delete(Item item) {
    print('delete: $item');
  }
  
  void add(Item item) {
    print('adds: $item');
  }
}
class NetworkRepository implements Repository {
class NetworkRepository implements Repository {
  
  @override 
  var items = <Item>[];
  
  @override
  void update(Item item) {
    print('network update: $item');
  }
  
  @override
  void delete(Item item) {
    print('network delete: $item');
  }
  
  @override
  void add(Item item) {
    print('network adds: $item');
  }
}
var repos = <Repository>[];
  repos.add(Repository());
  repos.add(NetworkRepository());
var item = Item(1);
for (var repo in repos) {
    repo.update(item);
}