Abstract Classes & Interfaces
Abstract classes and interfaces describe what objects can do without fixing how. They let you write code against a contract — "anything that can save()" — and swap implementations freely, which is the foundation of testable, flexible design.
Abstract classes
An abstract class can't be instantiated. It may contain abstract methods (no body) that concrete subclasses must implement, alongside normal methods and fields that subclasses inherit:
abstract class Notifier {
final String channel;
Notifier(this.channel);
// abstract: no body
bool deliver(String to, String message);
// concrete: shared by all subclasses
void broadcast(List<String> recipients, String message) {
var sent = 0;
for (final r in recipients) {
if (deliver(r, message)) sent++;
}
print('[$channel] sent $sent/${recipients.length}');
}
}
class SmsNotifier extends Notifier {
SmsNotifier() : super('SMS');
@override
bool deliver(String to, String message) {
if (!to.startsWith('+')) return false;
print(' SMS to $to: $message');
return true;
}
}
void main() {
SmsNotifier().broadcast(['+84901', 'bad-number', '+84902'], 'Sale ends today');
} SMS to +84901: Sale ends today
SMS to +84902: Sale ends today
[SMS] sent 2/3
Trying Notifier('x') would be a compile error: abstract classes can't be constructed.
Implicit interfaces
Dart has no interface keyword in the Java sense. Instead, every class implicitly defines an interface made of its public members. Any class can implements that interface — and then it must provide all of those members itself; nothing is inherited.
class Clock {
DateTime now() => DateTime.now();
}
// A test double that implements Clock's interface
class FixedClock implements Clock {
final DateTime fixed;
FixedClock(this.fixed);
@override
DateTime now() => fixed;
}
String greeting(Clock clock) {
final hour = clock.now().hour;
if (hour < 12) return 'Good morning';
if (hour < 18) return 'Good afternoon';
return 'Good evening';
}
void main() {
print(greeting(FixedClock(DateTime(2025, 6, 1, 9))));
print(greeting(FixedClock(DateTime(2025, 6, 1, 20))));
}Good morning
Good evening
This is how Dart code is typically made testable: depend on an interface, and pass a deterministic fake in tests.
Implementing several interfaces
A class can extend one class but implement many interfaces:
abstract interface class Identifiable {
String get id;
}
abstract interface class Printable {
String render();
}
class Invoice implements Identifiable, Printable {
@override
final String id;
final int amount;
Invoice(this.id, this.amount);
@override
String render() => 'Invoice #$id: $amount VND';
}
void printAll(List<Printable> items) {
for (final item in items) {
print(item.render());
}
}
void main() {
final inv = Invoice('A-17', 350000);
printAll([inv]);
Identifiable thing = inv;
print(thing.id);
}Invoice #A-17: 350000 VND
A-17
abstract interface class (Dart 3) declares a pure interface: it can't be instantiated (abstract) and can only be implemented, not extended (interface). See Class Modifiers.
extends vs implements
extends |
implements |
|
|---|---|---|
| How many | One class | Any number |
| Inherits code? | Yes — fields and method bodies | No — you write every member |
| Constructor chaining | Must call super |
No |
| Relationship | "is a specialised kind of" | "can be used as / fulfils the contract of" |
Forgetting a member when implementing is caught at compile time:
abstract class Repository {
void save(String item);
List<String> all();
}
class MemoryRepository implements Repository {
final _items = <String>[];
@override
void save(String item) => _items.add(item);
}
void main() {
MemoryRepository().save('x');
}example.dart:6:7: Error: The non-abstract class 'MemoryRepository' is missing implementations for these members:
- Repository.all
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 MemoryRepository implements Repository {
^^^^^^^^^^^^^^^^
example.dart:3:16: Context: 'Repository.all' is defined here.
List<String> all();
^^^
Error: AOT compilation failed
Bad state: Generating AOT kernel dill failed!
Designing with interfaces
A typical layered design:
abstract interface class PaymentGateway {
Future<bool> charge(int cents, String token);
}
class StripeGateway implements PaymentGateway { /* real HTTP calls */ }
class FakeGateway implements PaymentGateway { /* always succeeds, for tests */ }
class CheckoutService {
final PaymentGateway gateway; // depends on the abstraction
CheckoutService(this.gateway);
}Tip: Keep interfaces small and focused. Several narrow interfaces (
Readable,Writable) are easier to implement and fake than one giant interface.
Test yourself
What happens when a class implements another class?
- It inherits all method bodies
- It must provide its own implementation of every public member
- It can only implement one class
- It becomes abstract
Can you create an instance of an abstract class with Shape()?
- Yes
- No — it's a compile error
- Only with
new - Only if it has no abstract methods
Which Dart feature makes every class usable as an interface?
- Mixins
- Implicit interfaces
- Extension methods
- Generics