Skip to main content
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.

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.

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 ==.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More