Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract final class in Dart is a class modifier combination that establishes a strictly closed type hierarchy. It prevents the class from being instantiated globally, while simultaneously restricting inheritance (extends) and implementation (implements) exclusively to the library in which it is defined.

Technical Mechanics

The behavior of this construct is the exact intersection of its two modifiers:
  • abstract: Instructs the compiler to prohibit direct instantiation of the class. It permits the declaration of abstract members (methods, getters, setters) that lack an implementation, forcing valid subclasses to provide them.
  • final: Instructs the compiler to close the class to external derivation. No class outside of the defining library’s URI can use the class in an extends or implements clause. (Note: An abstract final class can never be used in a with clause, as Dart does not permit the final modifier on mixins or mixin classes).

Syntax and Scope Rules

Within the defining library (base_library.dart):
// base_library.dart

abstract final class CoreComponent {
  // Abstract member declaration allowed
  void initialize(); 
  
  // Concrete member allowed
  void dispose() {
    print('Disposed');
  }
}

// ALLOWED: Extending within the same library.
// Subtypes must be marked base, final, or sealed.
final class NetworkComponent extends CoreComponent {
  @override
  void initialize() => print('Network ready');
}

// ALLOWED: Implementing within the same library.
// Subtypes must be marked base, final, or sealed.
base class UIComponent implements CoreComponent {
  @override
  void initialize() => print('UI ready');
  
  @override
  void dispose() => print('UI disposed');
}
Outside the defining library (external_library.dart):
// external_library.dart
import 'base_library.dart';

void main() {
  // ERROR: Abstract classes cannot be instantiated.
  // CoreComponent component = CoreComponent(); 
}

// ERROR: The class 'CoreComponent' can't be extended outside of its library because it's a final class.
final class CustomComponent extends CoreComponent {
  @override
  void initialize() {}
}

// ERROR: The class 'CoreComponent' can't be implemented outside of its library because it's a final class.
base class MockComponent implements CoreComponent {
  @override
  void initialize() {}
  @override
  void dispose() {}
}

Compiler Characteristics

  • Constructors: An abstract final class can declare generative constructors. However, because the class cannot be instantiated directly, these constructors can only be invoked via super() calls from subclasses defined within the exact same library.
  • Exhaustiveness Checking: Unlike sealed classes (which are implicitly abstract and share the same library-restriction rules), abstract final classes do not trigger exhaustiveness checking in switch statements or expressions. The compiler will not enforce that all subtypes are handled when switching over an abstract final type.
  • Subtype Modifiers: Subclasses of an abstract final class within the same library must explicitly declare their own modifiers (base, final, or sealed) to maintain the integrity of the restricted hierarchy.
Master Dart with Deep Grasping Methodology!Learn More