Dart Class Modifiers
Dart Class Modifiers
A class modifier is a keyword that you write before class.
It controls what other code can do with your class, for example if it can be extended or implemented.
Dart has these class modifiers:
abstract: the class cannot be used to create objectsbase: the class can be extended, but not implementedinterface: the class can be implemented, but not extendedfinal: the class can be neither extended nor implementedsealed: the class has a fixed list of subclassesmixin: the class can also be used as a mixin
You already know abstract classes and sealed classes.
This chapter explains the other modifiers, and gives a summary of all of them at the end.
Modifiers and Libraries
Most class modifiers only limit code in other libraries.
In Dart, every .dart file is a library.
So the rules are about what other files can do with your class, for example when someone uses your package.
Inside the same file, you can still extend an interface class or a final class.
The examples on this page are single files, so they show what works inside one library.
The section Across Two Files shows the errors you get in another library.
Note: You will learn more about libraries and import in the Imports chapter.
base Classes
A base class makes sure that every subtype really inherits its code.
This is useful when the class has important code that must always run, like code that checks or saves data.
There is one rule that works even in the same file.
A class that extends a base class must itself be base, final or sealed.
Without one of these modifiers, the program does not run:
Example
base class Vehicle {
void move() => print('Moving');
}
class Car extends Vehicle {}
void main() {
Car().move();
}
Try it Yourself »
Result:
main.dart:5:7: Error: The type 'Car' must be 'base', 'final' or 'sealed' because the supertype 'Vehicle' is 'base'.
Try adding 'base', 'final', or 'sealed' to the type.
class Car extends Vehicle {}
^
Add base to the subclass, and it works:
Example
base class Vehicle {
void move() => print('Moving');
}
base class Car extends Vehicle {}
void main() {
Car().move();
}
Try it Yourself »
Result:
Moving
This rule makes sure that no class further down can be implemented by other libraries and skip the code of Vehicle.
interface and final Classes
An interface class is meant to be implemented.
Other libraries can implement it with implements, but they cannot extend it with extends.
You can still create objects of an interface class:
Example
interface class Printer {
void printPage() => print('Printing a page');
}
class LaserPrinter implements Printer {
@override
void printPage() => print('Laser printing a page');
}
void main() {
Printer().printPage();
LaserPrinter().printPage();
}
Try it Yourself »
Result:
Printing a page
Laser printing a page
A final class cannot be extended or implemented by other libraries at all.
Use it when you want to be sure that nobody else changes how the class works.
Inside the same file, you can still create subclasses, but just like with base, they must be base, final or sealed:
Example
final class Account {
double balance = 0;
void deposit(double amount) {
balance += amount;
}
}
final class SavingsAccount extends Account {
double rate = 0.03;
}
void main() {
var savings = SavingsAccount();
savings.deposit(100);
print(savings.balance);
print(savings.rate);
}
Try it Yourself »
Result:
100.0
0.03
Note: Do not mix up the class modifier final with a final variable.
A final variable cannot get a new value, and a final class cannot be extended or implemented outside its file.
Across Two Files
To see the library rules, you need two files.
That is why this example has no Try it Yourself button.
The first file, shop.dart, has three classes with modifiers:
shop.dart
base class Vehicle {
void move() => print('Moving');
}
interface class Printer {
void printPage() => print('Printing');
}
final class Account {
double balance = 0;
}
The second file, main.dart, imports shop.dart and tries to break each rule:
main.dart
import 'shop.dart';
base class Car implements Vehicle {
void move() => print('Car moving');
}
class LaserPrinter extends Printer {}
final class Savings extends Account {}
void main() {}
When we ran dart run main.dart, Dart showed these errors:
Result:
main.dart:3:27: Error: The class 'Vehicle' can't be implemented outside of its library because it's a base class.
base class Car implements Vehicle {
^
main.dart:7:28: Error: The class 'Printer' can't be extended outside of its library because it's an interface class.
class LaserPrinter extends Printer {}
^
main.dart:9:29: Error: The class 'Account' can't be extended outside of its library because it's a final class.
final class Savings extends Account {}
^
The same three classes in one file would give none of these errors.
mixin class
In Dart 3, a normal class cannot be used as a mixin:
Example
class Logger {
void log(String message) => print('LOG: $message');
}
class App with Logger {}
void main() {
App().log('Started');
}
Try it Yourself »
Result:
main.dart:5:16: Error: The class 'Logger' can't be used as a mixin because it isn't a mixin class nor a mixin.
class App with Logger {}
^
A mixin class can be used both as a normal class and as a mixin:
Example
mixin class Logger {
void log(String message) => print('LOG: $message');
}
class App with Logger {}
void main() {
App().log('Started');
Logger().log('Used as a normal class');
}
Try it Yourself »
Result:
LOG: Started
LOG: Used as a normal class
Summary
You can also combine some modifiers.
A common one is abstract interface class, which works like a pure interface in other languages:
Example
abstract interface class Payment {
void pay(double amount);
}
class CardPayment implements Payment {
@override
void pay(double amount) => print('Paid $amount by card');
}
void main() {
Payment p = CardPayment();
p.pay(25);
}
Try it Yourself »
Result:
Paid 25.0 by card
This table shows what each modifier allows.
The last three columns are about code in other libraries:
| Modifier | Create objects | Extend | Implement | Use as mixin |
|---|---|---|---|---|
class (no modifier) | Yes | Yes | Yes | No |
abstract class | No | Yes | Yes | No |
base class | Yes | Yes * | No | No |
interface class | Yes | No | Yes | No |
final class | Yes | No | No | No |
sealed class | No | No | No | No |
abstract interface class | No | No | Yes | No |
mixin class | Yes | Yes | Yes | Yes |
* The subclass must be base, final or sealed.
Tip: If you write a small program in one file, you rarely need these modifiers.
They are most useful when you write a library or package that other people use.