Implements v/s Extends v/s With Keywords In Dart — Stack Secrets

Sovit Poudel
Stack Secrets
Published in
4 min readMay 23, 2019

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.

Using 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.

abstract class X {
doX();
}

class Y implements X {
@override
doX() {
// TODO: implement doX
return null;
}
}

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.

Using 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.

class A {
doA() {
print(‘A’);
}
}

class B extends A {
}

main() {
var b = B();
b.doA();
}

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

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

class A {
doA() {
print(‘A’);
}
}

class B {
doB() {
print(‘B’);
}
}

// Not Allowed!
class C extends A, B {
}

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.

Using 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.

class D {
doD() {
print(‘d’);
}
}
class E with D {
}
main() {
var e = E();
e.doD();
}

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

class D {
D() {

}
doD() {
print(‘d’);
}
}

class E with D {

}

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.

mixin Z on D {
doZ() { }
}

class D {
doD() {
print(‘d’);
}
}

//Can’t do this!
class E with Z {

}

//Can do this!
class F extends D with Z {

}

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:

Originally published at http://stacksecrets.com on May 23, 2019.

--

--