Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Interfaces


Share

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.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.