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.

A base class in Dart is a class that establishes an inheritance hierarchy by allowing other classes to inherit its properties and methods. In Dart 3 and later, base is a specific class modifier that restricts how a class can be consumed across library boundaries, explicitly permitting inheritance (extends) while strictly prohibiting interface implementation (implements).

The base Modifier Mechanics

When a class is declared with the base modifier, Dart enforces the following compiler-level guarantees:
  1. Extension is permitted: External classes can use the extends keyword to inherit from the base class.
  2. Implementation is prohibited externally: Classes outside of the defining library cannot use the implements keyword to treat the base class as an interface.
  3. Modifier propagation: Any class that extends or implements a base class must be marked as base, final, or sealed. While implementing a base class is prohibited externally, it is perfectly valid within the same library. Enforcing the modifier on implementing classes ensures that external libraries cannot bypass the base restriction by implementing a subtype.
// library_a.dart
base class CoreSystem {
  int systemId;

  CoreSystem(this.systemId);

  void initialize() {
    print('System $systemId initialized');
  }
}

// VALID: Implementing a base class is allowed WITHIN the same library.
// The implementing class MUST propagate the restriction (base, final, or sealed).
final class InternalMockSystem implements CoreSystem {
  @override
  int systemId;

  InternalMockSystem(this.systemId);

  @override
  void initialize() {}
}
// library_b.dart
import 'library_a.dart';

// VALID: Extending a base class is allowed outside its library.
// The subclass MUST propagate the restriction (base, final, or sealed).
base class SubSystem extends CoreSystem {
  SubSystem(super.systemId);
}

// INVALID: Implementing a base class outside its library causes a compile-time error.
class ExternalMockSystem implements CoreSystem {
  // Compile error: The class 'CoreSystem' can't be implemented outside of its library
  // because it's a base class.
}

Abstract Base Classes

The base modifier can be combined with the abstract modifier. An abstract base class cannot be instantiated directly and forces subclasses to provide implementations for its abstract members, while still preventing external libraries from using it as an interface.
abstract base class DataProcessor {
  // Abstract method requiring implementation in subclasses
  void process(List<int> data);

  // Concrete method inherited by subclasses
  void log(String message) {
    print(message);
  }
}

// Subclasses must implement process() and must declare a valid modifier
final class ImageProcessor extends DataProcessor {
  @override
  void process(List<int> data) {
    // Implementation details
  }
}

The Implicit Base Class: Object

In the Dart type system, if a class does not explicitly extend another class, it implicitly extends Object. Therefore, Object serves as the ultimate base class for the entire Dart class hierarchy (with the exception of Null), providing foundational methods such as toString(), hashCode, and operator ==.
// This declaration:
class Entity {}

// Is semantically equivalent to:
class Entity extends Object {}
Master Dart with Deep Grasping Methodology!Learn More