"extends" versus "implements" versus "with"

extend can only be used with a single class at the time, BUT... you can easily extend a class which extends another class which extends another class which...! ;)

In fact, most Flutter widgets are already built like that.


Extends:

Use extends to create a subclass, and super to refer to the superclass.

Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.

You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.


Implements:

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.

Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.

When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.


With (Mixins):

Mixins are a way of reusing a class’s code in multiple class hierarchies.

With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.

They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.

Tags:

Dart