Skip to content
Blog

Dart Abstract Classes and Interfaces: Designing Contracts with abstract, interface, and implements

How to design type contracts in Dart: implicit interfaces, abstract classes, abstract interface classes, and when to extend vs implement.

Published on • December 4, 2026

AI Assistant

Every well-factored Dart codebase eventually needs a boundary: here is what I need from you, and I don’t care how you do it. Maybe it’s a repository that production implements with HTTP and tests implement with a fake. Maybe it’s a renderer that writes to a screen now and to a file later. Callers shouldn’t depend on the implementation — only on the contract.

Dart has no standalone interface keyword the way Java does. Instead, every class implicitly defines an interface, and the abstract and interface modifiers let you shape what that contract allows. Getting these choices right determines whether your API can be extended safely, mocked in tests, and evolved without breaking downstream code.

This post is a step-by-step tour of Dart’s contract tools: implicit interfaces, abstract classes, the interface and abstract interface modifiers, and how to decide between extends and implements in real code.

1. Implicit interfaces: every class is a contract

Every class implicitly defines an interface containing all of its instance members — and the instance members of any interfaces it implements. That means you can implements any class, not just one marked abstract:

// A person. The implicit interface contains greet().
class Person {
  final String _name; // In the interface, but visible only in this library.

  Person(this._name);

  // In the interface.
  String greet(String who) => 'Hello, $who. I am $_name.';
}

// An implementation of the Person interface — API only, no code reused.
class Impostor implements Person {
  String get _name => ''; // Must satisfy the interface's members.

  String greet(String who) => 'Hi $who. Do you know who I am?';
}

Key consequences:

  • implements copies the API, not the implementation. Impostor must provide every instance member of Person itself — there is no default code to inherit.
  • Constructors are not part of the interface. Only instance members (methods, getters, setters) are.
  • Private members like _name are part of the interface but only visible within their own library, so an implementing class in another library never has to satisfy them.

A class can implement several interfaces at once: class Point implements Comparable, Location { ... }.

2. Abstract classes: contracts with shared behavior

An abstract class is a class you can’t construct directly — you use it as a base for concrete subclasses to extend (or implement). It often mixes abstract members (declarations with no body) with concrete members that build on them:

abstract class Describable {
  void describe(); // Abstract: subclasses must implement.

  void describeWithEmphasis() {
    // Concrete: shared template method that calls the subclass's version.
    print('=========');
    describe();
    print('=========');
  }
}

Any class extending Describable inherits describeWithEmphasis() for free, and the template method calls the extender’s implementation of describe(). This is the classic “abstract method plus shared skeleton” pattern.

Attempting to instantiate an abstract class is a compile-time error in any library, including its own:

// Error: Vehicle can't be instantiated because it is marked abstract.
var v = Vehicle();

If you want a factory constructor that looks instantiable (returning a concrete subtype), declare one on the abstract class — that’s the standard escape hatch.

3. The interface modifier: implement, don’t extend

Plain classes let other libraries both extend and implement them. Sometimes that’s too permissive: if another library extends your class, its overrides can change what your own class’s methods do when they call each other on this — the classic fragile base class problem.

The interface modifier fixes that by forbidding external extends while still allowing implements:

// a.dart
interface class Vehicle {
  void moveForward(int meters) {
    // ...
  }
}

// b.dart
import 'a.dart';

Vehicle myVehicle = Vehicle(); // Can be constructed.

// Error: Vehicle can't be extended in a different library because
// it is marked with interface.
class Car extends Vehicle {
  int passengers = 4;
}

class MockVehicle implements Vehicle {
  @override
  void moveForward(int meters) {
    // Can be implemented.
  }
}

The guarantee: when a class’s own instance method calls another instance method on this, it always reaches a known implementation from the same library — external subclasses can’t override that path. This reduces the fragile base class problem.

4. abstract interface class: a pure contract

The most common use of interface is combined with abstract: an abstract interface class is a pure interface — can’t be constructed, can’t be extended from outside, but can be implemented anywhere:

abstract interface class Launchable {
  void launch();
}

class Rocket implements Launchable {
  @override
  void launch() => print('Liftoff!');
}

This is the closest Dart gets to Java’s interface keyword: a declaration whose only job is to specify an API.

5. Choosing between extends and implements

Both create subtypes, but they mean different things:

MechanismInherits implementation?Must implement members yourself?Use when
extendsYesOnly abstract membersYou are a specialized version of the parent; reuse its code
implementsNoYes — every instance memberYou satisfy the parent’s contract with unrelated implementation
with (mixin)Yes (of the mixin)Only abstract members of the mixinYou’re adding a behavior bundle, not joining a hierarchy — see the mixins post

A practical rule of thumb: if you’d answer “no” to is-a, don’t extends. A MockVehicle is not a Vehicle in the implementation sense — it just speaks the same language — so it implements:

// Extend for real reuse of shared code...
class Car extends Vehicle {
  @override
  void moveForward(int meters) {
    // ...
  }
}

// ...or implement for a fresh contract implementation.
// Error: Vehicle can't be instantiated because it is marked abstract.
// var v = Vehicle();
class MockVehicle implements Vehicle {
  @override
  void moveForward(int meters) {
    // You must write every member yourself.
  }
}

6. Modifier matrix: who may subtype, and how

Beyond abstract and interface, Dart 3’s class modifiers let you tighten the door further. The full set relevant to contracts:

ModifierConstruct outside lib?Extend outside lib?Implement outside lib?Typical use
(none)YesYesYesOrdinary class, unrestricted
abstractNoYesYesBase for subclasses; may hold abstract members
interfaceYesNoYesExtensible contract, implementation hidden
abstract interfaceNoNoYesPure interface
baseYesYes (must be base/final/sealed)NoForce inheritance to keep invariants
finalYesNoNoClose the hierarchy entirely
sealedNo (implicitly abstract)Same library onlySame library onlyEnumerable subtypes for exhaustive switches

Modifiers combine in a fixed order: optional abstract, then one of base/interface/final/sealed, then optional mixin, then class. For exhaustive coverage of base, final, and sealed, see the class modifiers post and the sealed classes post.

7. Designing a contract: a worked example

Suppose you’re defining a storage boundary. Step by step:

// Pure contract: no state, no implementation, implementable anywhere.
abstract interface class Cache<T> {
  T? get(String key);
  void set(String key, T value);
  void remove(String key);
}

// Concrete implementation: implements the contract, adds its own state.
class MemoryCache<T> implements Cache<T> {
  final Map<String, T> _store = {};

  @override
  T? get(String key) => _store[key];

  @override
  void set(String key, T value) => _store[key] = value;

  @override
  void remove(String key) => _store.remove(key);
}

Notice what the contract doesn’t contain: constructors, private fields, or implementation choices. A test’s FakeCache<T> implements the very same three members with zero shared code — consumers depend on Cache<T>, and producers decide how it works. If you later need shared behavior across implementations (say, a TTL layer), promote the contract to an abstract class with concrete helpers, or compose with a mixin.

Checklist

  • Prefer implements when the implementer shares no code with the contract — remember it copies API only, never implementation.
  • Use abstract class when you want shared concrete methods plus abstract members for subclasses to fill in.
  • Reach for abstract interface class when the type should be a pure contract: not constructible, not externally extendable.
  • Mark contract classes interface if external extends would let outsiders override methods your own library calls internally.
  • Never put constructors or private members in a contract that other libraries must implement.
  • Annotate implemented members with @override so signature drift is caught by the analyzer.
  • Choose extends only for a true is-a relationship with code reuse; choose implements for API conformance.
  • Combine modifiers in the documented order: abstract → (base|interface|final|sealed) → mixin → class.
  • For exhaustive subtype switching over a closed contract, use sealed instead of abstract interface.

Conclusion

Dart’s contract toolkit is built on one surprising fact — every class is already an interface — and two modifiers that refine it: abstract controls constructibility, interface controls external extension. Layer on implements, and you get a clean separation between what a type promises and how any particular implementation delivers.

The choice between extending for reuse and implementing for conformance is the daily decision point. Get it right, and your APIs stay mockable, testable, and safe to evolve; the modifier matrix in section 6 is the reference to keep nearby when the defaults aren’t restrictive enough.

References