Dart Interfaces
Dart Interfaces
An interface is a list of fields and methods that a class promises to have.
Unlike some other languages, Dart does not need a separate interface type.
Instead, every class is also an interface.
To use a class as an interface, write implements and the class name:
Example
abstract class Printable {
void printInfo();
}
class Book implements Printable {
String title;
Book(this.title);
@override
void printInfo() {
print('Book: $title');
}
}
void main() {
var book = Book('Dart for Beginners');
book.printInfo();
}
Try it Yourself »
Result:
Book: Dart for Beginners
An abstract class with only abstract methods, like Printable, is the usual way to write an interface in Dart.
You Must Implement Everything
When a class implements an interface, it gets no code from it.
The class must write its own version of every field and method in the interface.
This is true even when the interface is a normal class with method bodies:
Example
class Person {
String name = 'Unknown';
void greet() {
print('Hello from a person');
}
}
class Robot implements Person {
@override
String name = 'R2';
@override
void greet() {
print('Beep! I am $name');
}
}
void main() {
var robot = Robot();
robot.greet();
print(robot is Person);
}
Try it Yourself »
Result:
Beep! I am R2
true
If one member is missing, you get an error:
Example
class Person {
String name = 'Unknown';
void greet() {
print('Hello from a person');
}
}
class Robot implements Person {
@override
String name = 'R2';
}
void main() {
print(Robot().name);
}
Try it Yourself »
Result:
main.dart:9:7: Error: The non-abstract class 'Robot' is missing implementations for these members:
- Person.greet
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.
class Robot implements Person {
^^^^^
main.dart:4:8: Context: 'Person.greet' is defined here.
void greet() {
^^^^^
Implement Several Interfaces
A class can only extend one class, but it can implement many interfaces.
Separate the interface names with commas:
Example
abstract class Flyer {
void fly();
}
abstract class Swimmer {
void swim();
}
class Duck implements Flyer, Swimmer {
@override
void fly() {
print('The duck flies');
}
@override
void swim() {
print('The duck swims');
}
}
void main() {
var duck = Duck();
duck.fly();
duck.swim();
}
Try it Yourself »
Result:
The duck flies
The duck swims
You can also use extends and implements together.
Write extends first:
Example
class Animal {
String name;
Animal(this.name);
void eat() {
print('$name is eating');
}
}
abstract class Pet {
void play();
}
class Dog extends Animal implements Pet {
Dog(super.name);
@override
void play() {
print('$name plays with a ball');
}
}
void main() {
var dog = Dog('Buddy');
dog.eat();
dog.play();
}
Try it Yourself »
Result:
Buddy is eating
Buddy plays with a ball
Use the Interface as a Type
Different classes that implement the same interface can be used in the same way.
The code only needs to know the interface, not the real class:
Example
abstract class Payment {
void pay(double amount);
}
class CardPayment implements Payment {
@override
void pay(double amount) {
print('Paid $amount with a card');
}
}
class CashPayment implements Payment {
@override
void pay(double amount) {
print('Paid $amount in cash');
}
}
void checkout(Payment payment, double amount) {
payment.pay(amount);
}
void main() {
checkout(CardPayment(), 25.5);
checkout(CashPayment(), 10.0);
}
Try it Yourself »
Result:
Paid 25.5 with a card
Paid 10.0 in cash
extends or implements?
This table shows the main differences:
| extends | implements | |
|---|---|---|
| How many | Only one class | One or more interfaces |
| Gets the code (method bodies) | Yes | No |
| Must write every method | No, only abstract methods | Yes, all of them |
Can call its methods with super |
Yes | No |
| Object is also of that type | Yes | Yes |
Tip: Use extends when you want to reuse code.
Use implements when you only want to promise that a class has certain methods.