Sub topic 15
Class modifiers: abstract, base, interface, final, sealed, and mixin class
Assume you know Dart classes, constructors, inheritance, and null safety. Dart 3 class modifiers let you control how other code can use a class. They make design rules visible to the compiler.
Why modifiers matter
Without a modifier, a class can usually be extended, implemented, or used as a mixin according to the class's members. A modifier narrows those choices.
| Modifier | Can create objects? | Main restriction or guarantee |
|---|---|---|
| `abstract` | No | Declares incomplete behavior |
| `base` | Yes, if concrete | Outside libraries must extend, not implement |
| `interface` | Yes, if concrete | Outside libraries may implement, not extend |
| `final` | Yes, if concrete | Cannot be extended or implemented |
| `sealed` | No | Direct subtypes must be in the same library |
| `mixin class` | Yes, if concrete | Can be used with `extends` and `with` |
These rules apply mainly across library boundaries. Code in the same Dart library has more flexibility for some modifiers.
Abstract classes
An abstract class cannot be instantiated. It can contain both implemented methods and abstract members. Use it when a base type provides shared behavior but is incomplete by itself.
abstract class Shape {
double get area;
String description() => 'Area: $area';
}
class Square extends Shape {
final double side;
Square(this.side);
@override
double get area => side * side;
}
void main() {
final shape = Square(4);
print(shape.description());
}Shape requires subclasses to provide area. The @override annotation documents that Square fulfills an inherited contract.
Base, interface, and final classes
Use base class when subclasses must inherit the class's implementation. Code outside the library cannot implement it directly, so future changes to its implementation remain safer.
Use interface class when callers should depend on the public API but provide their own implementations. They can write implements, but they cannot extend the class outside its library.
Use final class when you want to prevent both extension and implementation. This is useful for value objects or APIs whose subtype behavior must never be customized.
Sealed classes
A sealed class is implicitly abstract. Its direct subclasses must be declared in the same library. This gives the analyzer a complete set of direct cases and supports exhaustive pattern matching.
sealed class Result {}
class Success extends Result {
final String value;
Success(this.value);
}
class Failure extends Result {
final String message;
Failure(this.message);
}
String display(Result result) => switch (result) {
Success(value: final value) => value,
Failure(message: final message) => 'Error: $message',
};
void main() {
print(display(Success('Saved')));
}Adding another direct subtype can require updating switches, which helps prevent forgotten cases.
Mixin classes
A mixin class can be used as a normal superclass or mixed into another class with with. Choose it when the same reusable behavior should support both forms.
mixin class Identifiable {
String id = '';
void printId() => print(id);
}
class Record with Identifiable {}
class User extends Identifiable {}
void main() {
final record = Record()..id = 'r1';
record.printId();
final user = User()..id = 'u1';
user.printId();
}Choosing a modifier
Use abstract for an incomplete shared base, interface for an implementation contract, base for controlled inheritance, final for a closed type, sealed for a known family of variants, and mixin class for behavior reusable through either inheritance or mixing in.
Exercise
Which modifier should you choose for a non-instantiable type with a fixed set of variants and exhaustive switching?
Show answer
Use sealed. Declare its direct subclasses in the same library.