Implicit Interfaces

Implicit Interfaces

About

  • Every class implicitly defines an interface.
  • No separate interface definition needed.
  • Enables duck typing.
  • Supports multiple interface implementation.

Main Topics

  1. Basic Implementation

    • Definition: implements keyword.
    • Example:
      class MockList implements List {...}
  2. Multiple Interfaces

    • Definition: Composing behaviors.
    • Example:
      class Employee implements Person, Identifiable {...}
  3. Partial Implementation

    • Definition: Must implement all members.
    • Example:
      class MockButton implements Button {
        @override void onPress() {...}
        // Must implement all Button members
      }
  4. Testing Advantages

    • Definition: Easy mock creation.
    • Example:
      class MockAuth implements AuthService {...}

How to Use

  • Implement: Any class as interface
  • Compose: Multiple class interfaces
  • Mock: Create test doubles easily
  • Document: Interface expectations

How It Works

  • Contracts: All public members become interface
  • Verification: Compiler enforces completeness
  • Flexibility: No formal interface definition needed

Example Session:

void main() {
  var mock = MockList(); // Implements List interface
  print(mock.length);
}

Conclusion

Dart’s implicit interfaces provide flexible contract implementation without ceremony, particularly valuable for testing and adapting existing classes to new interfaces.