Skip to main content
The base modifier restricts a class to implementation inheritance. It disables the class’s implicit interface for external libraries, guaranteeing that all instances of the class or its subtypes contain the underlying concrete implementation and initialized state. This ensures that private state defined in the base class is instantiated, even though private members are not visible to or inherited by subclasses.

Syntax Declaration

base class Vehicle {
  void move() {}
}

Core Constraints

The base modifier imposes the following technical restrictions on the class and its dependents:
  1. Prohibited Interface Implementation: External libraries cannot use the implements clause with a base class. The compiler enforces that the class contract is fulfilled via inheritance (extends) rather than reimplementation.
  2. Mixin Restrictions: A declaration of base class prevents usage in a with clause. However, a class declared as base mixin class permits mixin usage, provided the consuming class adheres to subtype modifier restrictions.
  3. Transitive Subtype Restriction: Any class that extends (or mixes in) a base type must itself be marked with one of the following modifiers to control further inheritance:
    • base
    • final
    • sealed

Inheritance Mechanics

Because base enforces implementation inheritance, it prevents the creation of a subtype that bypasses the base class’s constructor or field initialization.

Valid Inheritance

A subclass extending a base class maintains the implementation contract and must declare its own restrictive modifier.
// defined in library_a.dart
base class Processor {
  void process() => print('Processing');
}

// defined in library_b.dart
// Valid: Uses 'extends' and marks subclass as 'base'
base class AudioProcessor extends Processor {
  @override
  void process() => super.process();
}

// Valid: Uses 'extends' and marks subclass as 'final'
final class VideoProcessor extends Processor {}

Invalid Operations

The compiler will emit static analysis errors for the following operations when performed outside the defining library:
// defined in library_a.dart
base class Processor {}

// defined in library_b.dart

// Error: Cannot implement a base class; implementation inheritance is required
class MockProcessor implements Processor {} 

// Error: Subtypes of a base class must be base, final, or sealed
class UnrestrictedProcessor extends Processor {}

// Error: A standard 'base class' cannot be used as a mixin
class HybridProcessor with Processor {}

Base Mixins and Mixin Classes

While a standard base class cannot be mixed in, the base modifier can be applied to mixins or mixin classes. This enforces the same transitive subtype restrictions: any class applying the mixin must be declared base, final, or sealed.
// defined in library_a.dart
base mixin class Logger {
  void log() {}
}

// defined in library_b.dart

// Valid: Consuming class is marked 'base'
base class Service with Logger {}

// Error: The class 'Job' must be base, final, or sealed because it uses a base mixin
class Job with Logger {}
Master Dart with Deep Grasping Methodology!Learn More