CodeBlanks Logo Icon CodeBlanks

OOP model

Set up the class hierarchy and interfaces.

OOP model

We add an interface to the Task class. In Dart an abstract class defines an interface; you implement it with implements.

abstract class Trackable { bool get isDone; } declares an abstract getter. You implement it with Task implements Trackable.

In the implementation you add @override bool get isDone => done;. This shows the OOP hierarchy and polymorphism.

Applying with @override

The Task class now says implements Trackable; it applies the abstract getter with @override bool get isDone => done;.

The @override annotation tells the compiler that a member of a parent class or interface is being overridden. This turns a typo into a compile error.

So every Task is now a Trackable; code expecting a Trackable can accept a Task (polymorphism).

Practice Trackable interface and Task

Define abstract class Trackable { bool get isDone; }. Make Task implements Trackable and add @override bool get isDone => done;. In main create var t = Task('Test write', done: true); and print print(t.isDone).

Starter code

main.dart
// Define the Trackable interface

class Task {
  final String title;
  bool done;
  Task(this.title, {this.done = false});
}

void main() {
  // create a Task instance and print isDone
}

Sample solution

main.dart
abstract class Trackable {
  bool get isDone;
}

class Task implements Trackable {
  final String title;
  bool done;
  Task(this.title, {this.done = false});

  @override
  bool get isDone => done;
}

void main() {
  var t = Task('Test write', done: true);
  print(t.isDone);
}

Expected output

true

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Trackable interface

Question
abstract class Trackable { bool get ____; }
Answer
abstract class Trackable { bool get isDone; }

Explanation: the abstract getter isDone is declared.

2 How does Task implement Trackable?

  • Task(Trackable)
  • Task extends Trackable
  • Task with Trackable
  • Task implements Trackable Correct answer

Explanation: implements is Dart's keyword for applying an interface.

3 OOP model parts

Term Meaning
abstract class Interface
implements Implementation
@override Override
get isDone Getter

All lessons in Intermediate