Python Class Modifiers: sealed, final, base & interface
Dart 3 introduced fine-grained **Class Modifiers** to control how classes can be used across library boundaries. **`sealed`** creates closed algebraic data type hierarchies with exhaustive pattern matching; **`final`** disallows external subtyping; **`base`** enforces inheritance without implementation; and **`interface`** allows implementation without extension.
"Class modifiers are like property zoning laws: "Residential Only" (interface) means you can build a house that looks like the blueprint, while "Historical Landmark" (final) means nobody is allowed to alter or extend the structure."
Deep Dive: How It Works
`sealed`: All subclasses must be declared in the same library file. Enables compiler exhaustiveness in switch expressions.
`final`: Outside the defining library, the class cannot be extended, implemented, or mixed in.
`base`: Outside the defining library, classes can only `extend` (preserving base constructor invariants), disallowing `implements`.
`interface`: Outside the defining library, classes can only `implements`, disallowing `extends`.
Syntax Blueprint
// Sealed hierarchy for state
sealed class ViewState {}
class Loading extends ViewState {}
class Success extends ViewState { final String data; Success(this.data); }
class Failure extends ViewState { final String err; Failure(this.err); }Sealed class establishing an exhaustive state hierarchy.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to extend a `sealed` class in a different `.dart` file.Why it happens: Sealed classes require all subtypes to be defined in the same file to guarantee compile-time exhaustiveness.
How to fix: Define all subtypes of the sealed class in the same file.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Define Sealed Auth State
Define `sealed class AuthState {}`.
Define `class AuthLoggedIn extends AuthState {}` and `class AuthLoggedOut extends AuthState {}`.
Write function `String getAuthText(AuthState s) => switch (s) { AuthLoggedIn() => "IN", AuthLoggedOut() => "OUT" };`.
In `main()`, print `Auth: ${getAuthText(AuthLoggedIn())}`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.