final modifier restricts the inheritance hierarchy of a class to its defining library, completely closing the class to external extension or implementation while permitting unrestricted instantiation.
Syntax
Core Semantics
When a class is marked asfinal, the Dart type system enforces the following constraints:
- Instantiation: The class can be constructed (instantiated) from any library, provided the constructor is accessible (public).
- External Inheritance: Classes outside the defining library cannot
extend,implement, or use the class as amixin. - Internal Inheritance: Classes within the same library can extend or implement the
finalclass.
Subtype Constraints
To maintain the closure guarantees of the type system, any class extending or implementing afinal class within the same library must also be marked with one of the following modifiers:
basefinalsealed
final class from being bypassed by a less restrictive subtype.
Behavior Visualization
File:shapes.dart (Defining Library)
main.dart (External Library)
API Surface Guarantee
Because external libraries cannot create new subtypes, thefinal modifier guarantees that:
- Instance Integrity: Any instance of type
T(whereTis final) originated from the defining library or is a direct instance ofT. - Method Resolution: Method overrides cannot be introduced by external code, ensuring that calls to instance methods execute logic defined solely within the library’s scope.
Master Dart with Deep Grasping Methodology!Learn More





