Final Class
To close the type hierarchy, use the final modifier. This prevents subTyping
from a class outside of the current library.Disallowing both inheritance andimplementation prevents subTyping entirely.
You can safely add incremental change to the API.
You can call instance methods knowing that they haven't been overwritten in a third-party subclass.
final class Vehicle {
void moveForward(int meters) {
// ...
}
}
import 'a.dart';
// Can be constructed.
Vehicle myVehicle = Vehicle();
// ERROR: Can't be inherited.
class Car extends Vehicle {
int passengers = 4;
// ...
}
class MockVehicle implements Vehicle {
// ERROR: Can't be implemented.
@override
void moveForward(int meters) {
// ...
}
}