Implements v/s Extends v/s With Keywords In Dart

In this post, we will go cover use cases for implements, extends and with keywords. Implements v/s Extends v/s With keywords in Dart.

Introduction

If you are developing a Flutter application, you should have seen a lot of use cases for implements, extends and with keywords. For someone who has never worked with Dart before, the difference between these keywords is not obvious.

In this post, we will go in depth about these keywords to expand our understanding of Flutter and Dart.

Implements Is For Forcing Behavior Via Interface

In OOP, an interface is something that enforces the deriving class to implement a set list of public fields and methods.

But unlike other traditional programming languages like C# and JAVA, Dart does not have explicit interface types. Each class, by default, defines its own interface composed of public fields and methods. So, every class can act as an interface in Dart.

We use the implements keyword to implement an interface. Also, a class can implement multiple interfaces.

Dart Example 1 Implements
Dart Example 1 Implements

In the example above, class D implements classes A, B and C therefore it must implement the public methods and fields of those classes.

Furthermore, since Dart does not have explicit interfaces, we use the abstract class as an interface.

Extends Is For Sharing Behavior Via Inheritance

In OOP, inheritance implies sharing of behavior between classes. We can not share features with an interface. So, when we implement a class, we can not share it’s behavior.

Dart Example Extends
Dart Example Extends

In the example above, you can see that class B needs to provide it’s own implementation of doA() method as the behavior is not shared.

If you want to share the behavior across these two classes, you should use the extends keyword.

In the above example, since B extends A, you can call the doA() method directly from B’s object.

Become A Flutter Expert With This Course

Unlike implementing multiple interfaces, Dart only supports single inheritance. So, you can not extend from multiple classes.

With Is For Mixin

In OOP, a mixin is a class that contains methods for use by other classes. Unlike the interface and inheritance approach, a mixin doesn’t have to be the parent class of those other classes.

So a mixin neither imposes usage restriction nor forces type restriction.

You will usually put common functions inside a mixin. In Dart, we make use of the mixin by using the with keyword.

Dart Example With
Dart Example With

As shown in the example above, implementation of mixin fields can be overridden if required. Furthermore, a class can use multiple mixins.

Few More Things To Note About Mixin

Using A Class As A Mixin

In Dart, a class can also be used as mixin if the class is constructor-less. For example, the code below is valid.

But this one below is invalid because class D declares a constructor.

Enforcing Mixin For Specific Types

You can also enforce a mixin to be usable for certain types classes only by using the the on keyword when defining the mixin.

Conclusion

In this post we learned about the use cases for implements, extends and with keywords when developing Flutter/Dart applications.

Implements v/s Extends v/s With

To sum up use:

  • Implements for enforcing behaviors.
  • Extends for sharing behaviors of single class.
  • Mixins for sharing behaviors from multiple reusable types.

Learn More:

About the author