Dart Interfaces


Interfaces in Dart

In Dart, interfaces are a way to define a contract that a class must follow. Unlike some other languages, Dart doesn’t have a separate interface keyword. Instead, every class in Dart can act as an interface. This means that when a class implements another class, it agrees to provide an implementation for all of that class's methods and properties.

Interfaces are particularly useful for defining common behavior across multiple, possibly unrelated classes. You can create an interface by simply creating a class with method definitions and then implementing it in other classes.

Key Points:

  • In Dart, any class can be used as an interface.
  • To use a class as an interface, another class must implement it.
  • The implementing class must provide concrete implementations for all methods and properties defined in the interface.
  • Interfaces help define consistent method signatures that multiple classes can adopt, promoting polymorphism and code reusability.

Example of an Interface in Dart

Suppose we want to define an interface for a device that can be turned on and off. We’ll create a Switchable interface and then have two classes, Light and Fan, implement it.

Code Example:

// Interface class with methods to implement class Switchable { // Method signatures (no implementation) void turnOn(); void turnOff(); } // Class implementing the Switchable interface class Light implements Switchable { // Implementing turnOn method @override void turnOn() { print('The light is now ON'); } // Implementing turnOff method @override void turnOff() { print('The light is now OFF'); } } // Another class implementing the Switchable interface class Fan implements Switchable { // Implementing turnOn method @override void turnOn() { print('The fan is now ON'); } // Implementing turnOff method @override void turnOff() { print('The fan is now OFF'); } } void main() { // Creating instances of classes that implement the Switchable interface Switchable light = Light(); Switchable fan = Fan(); // Using methods defined by the interface light.turnOn(); // Output: The light is now ON light.turnOff(); // Output: The light is now OFF fan.turnOn(); // Output: The fan is now ON fan.turnOff(); // Output: The fan is now OFF }

Explanation:

  1. Interface Definition:

    • Switchable acts as an interface because it defines two method signatures: turnOn() and turnOff(). There is no implementation for these methods within Switchable.
  2. Implementing the Interface:

    • Both Light and Fan classes implement the Switchable interface by using the implements keyword.
    • Each class provides its own concrete implementation for the turnOn() and turnOff() methods.
  3. Usage:

    • In the main() function, we create instances of Light and Fan and assign them to a Switchable type variable.
    • Each instance then calls turnOn() and turnOff(), using the method signatures defined by the Switchable interface, but each with its own specific implementation.
  4. Polymorphism:

    • The Switchable type allows us to write code that can handle different types (such as Light and Fan) in a uniform way, as long as they implement the Switchable interface.

Output:

The light is now ON The light is now OFF The fan is now ON The fan is now OFF

Benefits of Using Interfaces:

  1. Consistent APIs: Interfaces provide a contract that classes must follow, ensuring consistency across implementations.
  2. Decoupling and Flexibility: Interfaces help separate the definition of behavior from implementation, making it easier to change one without affecting the other.
  3. Polymorphism: Interfaces allow you to write more flexible and reusable code, as classes implementing the same interface can be used interchangeably.

Summary:

In Dart, interfaces are defined by creating classes with method signatures and then using the implements keyword in other classes. Each implementing class must provide concrete implementations for all methods defined in the interface. This approach allows for polymorphism, code reusability, and consistency across different classes in your application.