Dart Lesson 27 – Interfaces | Dataplexa

Interfaces in Dart

In this lesson, you will learn about interfaces in Dart. Interfaces allow you to define a contract that a class must follow.

They are a key concept for building scalable, flexible, and maintainable applications.


What Is an Interface?

An interface defines what a class should do, not how it should do it.

In Dart, there is no separate keyword called interface. Instead, every class can act as an interface.


How Interfaces Work in Dart

  • Dart uses the implements keyword
  • All methods must be implemented
  • No method body is inherited

When a class implements another class, it must override all methods.


Real-World Example Scenario

Imagine different notification systems in an application:

  • Email notifications
  • SMS notifications
  • Push notifications

All of them must send messages, but each works differently.


Creating an Interface

First, create a class that will act as an interface.

class NotificationService {
  void sendMessage(String message) {}
}

This class will now act as an interface.


Implementing an Interface

class EmailNotification implements NotificationService {
  @override
  void sendMessage(String message) {
    print('Email sent: $message');
  }
}

Another Interface Implementation

class SmsNotification implements NotificationService {
  @override
  void sendMessage(String message) {
    print('SMS sent: $message');
  }
}

Using Interfaces in Real Code

You can use interface references to write flexible code.

void main() {
  NotificationService service1 = EmailNotification();
  service1.sendMessage('Welcome to Dataplexa');

  NotificationService service2 = SmsNotification();
  service2.sendMessage('Your OTP is 123456');
}

This makes your application easily extendable.


Implementing Multiple Interfaces

A class can implement multiple interfaces in Dart.

class Logger {
  void log(String message) {}
}

class FileLogger implements NotificationService, Logger {
  @override
  void sendMessage(String message) {
    print('Logged message: $message');
  }

  @override
  void log(String message) {
    print('Writing to log file: $message');
  }
}

Interface vs Abstract Class

  • Interfaces require full implementation
  • Abstract classes can have implemented methods
  • Interfaces support multiple inheritance

When Should You Use Interfaces?

  • When you need multiple inheritance
  • When you want strict contracts
  • When building frameworks or APIs

Common Mistakes

  • Forgetting to implement all methods
  • Using extends instead of implements
  • Not using interface references

📝 Practice Exercises


Exercise 1

Create an interface Vehicle with a method move().

Exercise 2

Create classes Car and Bike that implement it.

Exercise 3

Call the move() method for both objects.


✅ Practice Answers


class Vehicle {
  void move() {}
}

class Car implements Vehicle {
  @override
  void move() {
    print('Car is moving on the road');
  }
}

class Bike implements Vehicle {
  @override
  void move() {
    print('Bike is moving through traffic');
  }
}

void main() {
  Vehicle v1 = Car();
  Vehicle v2 = Bike();

  v1.move();
  v2.move();
}

What’s Next?

In the next lesson, you will learn about Mixins in Dart — a powerful way to reuse functionality across multiple classes.