Menu
Coddy logo textTech

Interfaces in Dart

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 45 of 110.

Unlike many languages that have a dedicated interface keyword, Dart takes a different approach. In Dart, every class automatically defines an interface. This interface consists of all the public members (methods and properties) that the class declares.

When you want a class to promise it will provide certain functionality without inheriting implementation, you use the implements keyword:

class Printer {
  void printDocument(String content) {
    print('Printing: $content');
  }
}

class Scanner {
  void scan() {
    print('Scanning document');
  }
}

// MultiFunctionDevice must implement ALL methods from both classes
class MultiFunctionDevice implements Printer, Scanner {
  @override
  void printDocument(String content) {
    print('MFD printing: $content');
  }
  
  @override
  void scan() {
    print('MFD scanning');
  }
}

The key difference from extends is that implements doesn't inherit any code. You must provide your own implementation for every method and property defined in the interface. This is a contract - you're promising your class will have these capabilities, but you decide exactly how they work.

This approach gives Dart flexibility. You can use abstract classes when you want to define interfaces with no implementation, or use regular classes as interfaces when it makes sense. The next lesson explores how this "implicit interface" concept works in more detail.

challenge icon

Challenge

Easy

Let's build a media player system that demonstrates how the implements keyword works in Dart. You'll create classes that serve as interfaces and then implement them in a single device that combines multiple capabilities.

You'll organize your code into two files:

  • media.dart: Define your interface classes and the implementing class here:
    • An AudioPlayer class with a method playAudio(String track) that prints Playing audio: [track]
    • A VideoPlayer class with a method playVideo(String title) that prints Playing video: [title]
    • A SmartTV class that implements both AudioPlayer and VideoPlayer. Since implements doesn't inherit any code, you must provide your own implementations for both methods. Your SmartTV should print SmartTV audio: [track] for audio and SmartTV video: [title] for video
  • main.dart: Import your media file and demonstrate how a single class can implement multiple interfaces:
    • Create a SmartTV instance
    • Call playAudio('Jazz Collection')
    • Call playVideo('Nature Documentary')

Notice how SmartTV must provide its own implementation for every method from both interfaces - it doesn't inherit the original implementations from AudioPlayer or VideoPlayer. This is the key difference between implements and extends.

Expected output:

SmartTV audio: Jazz Collection
SmartTV video: Nature Documentary

Cheat sheet

In Dart, every class automatically defines an interface consisting of all its public members. To implement an interface without inheriting code, use the implements keyword:

class Printer {
  void printDocument(String content) {
    print('Printing: $content');
  }
}

class Scanner {
  void scan() {
    print('Scanning document');
  }
}

// Must implement ALL methods from both classes
class MultiFunctionDevice implements Printer, Scanner {
  @override
  void printDocument(String content) {
    print('MFD printing: $content');
  }
  
  @override
  void scan() {
    print('MFD scanning');
  }
}

Key differences from extends:

  • implements doesn't inherit any code implementation
  • You must provide your own implementation for every method and property
  • A class can implement multiple interfaces
  • It's a contract promising your class will have these capabilities

Try it yourself

import 'media.dart';

void main() {
  // TODO: Create a SmartTV instance
  
  // TODO: Call playAudio('Jazz Collection')
  
  // TODO: Call playVideo('Nature Documentary')
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming