Ahmet Balaman LogoAhmet Balaman

Dart Interface and Implements - Adding Extra Features to Classes

personAhmet Balaman
calendar_today
DartInterfaceAbstractOOPFlutter

Interfaces and Implements - Abstract Concepts

Today I got into more advanced Dart topics: Abstract classes and implements. This topic seemed quite abstract at first (abstract means abstract after all), but it started to become clear as I worked through examples.

Live Demo: Abstract Class and Implements

Learn abstract class and implements usage interactively:

What is an Abstract Class?

An abstract class is a class that you can't create an object from on its own, but provides a template for other classes. Think of it like this: You have a recipe book, but you can't eat from the recipe book. You first need to make the food from that recipe.

abstract class Squeezable {
  void howToSqueeze() {}
}

abstract class Eatable {
  void howToEat() {}
}

Here I defined two abstract classes. One for "squeezable things", the other for "edible things".

Adding Features to Classes with Implements

Unlike extends, when you use implements, you can get features from multiple classes. I call this "signing a contract". You're promising "I will implement these features".

Apple Example

abstract class Squeezable {
  void howToSqueeze() {}
}

abstract class Eatable {
  void howToEat() {}
}

class Apple implements Eatable, Squeezable {
  @override
  void howToEat() {
    print("Bite with your mouth and chew");
  }
  
  @override
  void howToSqueeze() {
    print("Put in juicer and squeeze");
  }
}

void main() {
  var apple1 = Apple();
  
  print("How to eat an apple?");
  apple1.howToEat();
  
  print("\nHow to squeeze an apple?");
  apple1.howToSqueeze();
}

Output:

How to eat an apple?
Bite with your mouth and chew

How to squeeze an apple?
Put in juicer and squeeze

Why Two Interfaces?

Because you can both eat an apple and squeeze it for juice. But think about a stone: A stone isn't edible but maybe breakable. Then the Stone class would only implement the Breakable interface.

Difference Between Extends vs Implements

I was confusing these two at first, now I've clarified:

Extends Implements
Inherits from single class Can take from multiple interfaces
Inherits features from parent Must fulfill the contract
Optional override Must override

Example Explanation

// EXTENDS example
class Animal {
  void breathe() {
    print("Breathing");
  }
}

class Dog extends Animal {
  // We automatically got the breathe method
  // We can override if we want, or not
}

// IMPLEMENTS example
abstract class Walkable {
  void walk();
}

class Human implements Walkable {
  @override
  void walk() {
    // We MUST write this
    print("Human is walking");
  }
}

Real-Life Flutter Example

We actually use this structure all the time when writing widgets in Flutter:

abstract class Clickable {
  void whenClicked();
}

abstract class Scrollable {
  void whenScrolled();
}

class CustomButton implements Clickable {
  @override
  void whenClicked() {
    print("Button clicked!");
    // Send to analytics
    // Start animation
    // Save data etc.
  }
}

class CustomListView implements Clickable, Scrollable {
  @override
  void whenClicked() {
    print("List item clicked");
  }
  
  @override
  void whenScrolled() {
    print("List scrolled");
  }
}

As you can see, CustomListView can both be clicked and scrolled. If we had used extends, we could only get features from one class.

IDE Help

When you use VS Code or Android Studio, when you implement an interface, the IDE automatically adds the methods for you. This is very useful:

  1. You write implements Eatable
  2. A red line appears (there's an error)
  3. You click the light bulb icon
  4. You say "Implement members"
  5. All methods are automatically added!

Can I Delete Unused Methods?

Technically you can't delete them, because implements is a contract. But you can leave them empty:

class Orange implements Eatable, Squeezable {
  @override
  void howToEat() {
    print("Peel and eat");
  }
  
  @override
  void howToSqueeze() {
    // Left empty, but we had to write it
  }
}

Or better yet, if you really don't need it, don't implement that interface.

What Did I Learn Today?

  • Abstract classes serve as templates
  • Implements allows us to use multiple interfaces
  • I understood the difference between extends and implements
  • IDE's auto-completion feature is very useful

Tomorrow I'll look at Collections (List, Set, Map). They're also very important in Flutter.

Confusing Point

You can both extend and implement an abstract class. The difference:

  • If you extend: You can use the methods inside (override optional)
  • If you implement: You must override everything
abstract class Shape {
  void draw() {
    print("Drawing shape");
  }
}

// With extends
class Square extends Shape {
  // We can use the draw() method, override is optional
}

// With implements
class Circle implements Shape {
  @override
  void draw() {
    // We MUST write this
    print("Drawing circle");
  }
}

It took me a while to understand this detail but now it's clear.


If you want to work with me:

See you in the next post!