Skip to main content
An abstract interface class is a class declaration modifier that defines a strict type contract. It combines the constraints of the abstract keyword and the interface modifier to create a type that cannot be instantiated and can only be adopted via implementation, prohibiting inheritance via extension outside of its defining library.

Syntax

abstract interface class Vehicle {
  void move();
  String get id;
}

Semantic Rules

The abstract interface class construct enforces the following technical constraints:
  1. Instantiation: The class cannot be instantiated. Attempting to create an instance via a constructor results in a compile-time error.
  2. External Inheritance: Classes outside the defining library cannot inherit from this class using the extends keyword.
  3. Implementation: Classes (both internal and external to the library) must use the implements keyword to adopt the type.
  4. Internal Inheritance: Within the defining library, the class may still be extended. This applies to the entire library scope, including parts spread across multiple files.

Member Implementation Behavior

Unlike a standard interface in some other languages, a Dart abstract interface class may contain method bodies and fields. However, because the consuming class must use implements:
  • The implementing class does not inherit the logic or state of the interface.
  • Concrete implementing classes must override every field and method defined in the interface.
  • Abstract implementing classes are not required to implement members immediately and may defer implementation to their subclasses.
  • The implementing class cannot access super to invoke the interface’s default behavior.

Code Example

Library Definition (storage.dart):
// Definition of the abstract interface
abstract interface class Storage {
  void save(String data);
  
  // Concrete method body is allowed but not inherited via 'implements'
  void log() {
    print('Default logging');
  }
}
Consumer Usage (main.dart):
import 'storage.dart';

// ✅ Valid: Concrete class implements the interface
class CloudStorage implements Storage {
  @override
  void save(String data) {
    // Implementation logic
  }

  @override
  void log() {
    // Must be overridden in a concrete class; cannot call super.log()
    print('Cloud logging');
  }
}

// ✅ Valid: Abstract class implements the interface
abstract class PartialStorage implements Storage {
  // Not required to override save() or log() here
}

// ❌ Invalid: Cannot extend outside the library
class LocalStorage extends Storage { 
  // Compile-time error: The class 'Storage' can't be extended outside of its library 
  // because it's an interface class.
}

void main() {
  // ❌ Invalid: Cannot instantiate
  final store = Storage(); 
  // Compile-time error: The class 'Storage' is abstract and can't be instantiated.
}
Master Dart with Deep Grasping Methodology!Learn More