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 interface class in Dart is a class modifier that permits external libraries to implement the class’s API while strictly prohibiting them from extending it through inheritance. This enforces a contract where external consumers can only provide their own implementations of the interface, ensuring the original class’s internal implementation details and inheritance hierarchy remain isolated to its defining library.

Syntax

The interface modifier is placed directly before the class keyword. It can be used on its own to create a concrete class, or combined with the abstract modifier to create an uninstantiable contract.
// Concrete interface class: Can be instantiated.
interface class ConcreteInterface {
  void execute() => print('Executing');
}

// Abstract interface class: Cannot be instantiated.
abstract interface class PureInterface {
  void execute();
}

Technical Rules and Behaviors

  1. Implementation (implements): Any class, regardless of the library it resides in, can implement an interface class. The implementing class must define all members of the interface.
  2. Inheritance (extends): A class located in a different library cannot extend an interface class. Attempting to do so results in a compile-time error.
  3. Same-Library Exemption: Within the exact same library (file) where the interface class is declared, the class can be extended. The inheritance restriction only applies across library boundaries.
  4. Instantiation: Unlike interfaces in languages like Java or C#, a Dart interface class can be instantiated directly using its constructor, provided it is not marked with the abstract modifier.

Boundary Enforcement Example

The following example demonstrates the compiler enforcement of the interface class across library boundaries.
// --- library_a.dart 
interface class Processor {
  void process() => print('Default processing');
}

// VALID: Extending is permitted within the same defining library.
class SubProcessor extends Processor {
  @override
  void process() => print('Sub processing');
}


// --- library_b.dart 
import 'library_a.dart';

// VALID: External libraries can implement the interface class.
class CustomProcessor implements Processor {
  @override
  void process() => print('Custom processing');
}

// ERROR: External libraries cannot extend an interface class.
// class InvalidProcessor extends Processor {} 

// VALID: Concrete interface classes can be instantiated externally.
void main() {
  Processor defaultProcessor = Processor(); 
}

Modifier Compatibility

The interface modifier dictates the external inheritance contract. Therefore, it is mutually exclusive with other modifiers that dictate external inheritance or implementation rules:
  • Valid combinations: abstract interface class
  • Invalid combinations: base interface class, final interface class, sealed interface class (these result in syntax errors).
Master Dart with Deep Grasping Methodology!Learn More