We have here a superclass called Animal which has three subclasses (Mammal, Bird, and Fish). At the bottom, we have concrete classes.

The little squares represent behavior. For example, the blue square indicates that an instance of a class with this behavior can swim.

Some animals share common behavior: A cat and a dove can both walk, but the cat cannot fly (except for Nyan Cat 😀).

These kinds of behavior are orthogonal to this classification, so we cannot implement these behavior in the superclasses.

If a class could have more than one superclass, it would be easy, we could create three other classes: Walker, Swimmer, Flyer. After that, we would just have to inherit Dove and Cat from the Walker class. But in Dart, every class (except for Object ) has exactly one superclass.

Instead of inheriting from the Walker class, we could implement it, as it if was an interface, but we should have to implement the behavior in multiple classes, so it’s not a good solution.

We need a way of reusing a class’s code in multiple class hierarchies.

You know what? Mixins are exactly that:

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

Described like that, it sounds easy 😁.

🔒 Constraints

The mixin feature comes with a few restrictions (from dartlang.org):

Dart 1.12 or lower supports mixins that must extend Object , and must not call super() .

, and must not call . Dart 1.13 or greater supports mixins that can extend from classes other than Object , and can call super.method() . This support is only available by default in the Dart VM and in Analyzer behind a flag. More specifically, it is behind the --supermixin flag in the command-line analyzer. It is also available in the analysis server, behind a client-configurable option. Dart2js and dartdevc do not support super mixins.

, and can call . This support is only available by default in the Dart VM and in Analyzer behind a flag. More specifically, it is behind the flag in the command-line analyzer. It is also available in the analysis server, behind a client-configurable option. Dart2js and dartdevc do not support super mixins. In Dart 2.1, mixins are expected to have fewer restrictions. For example, Flutter supports mixins calling super() and extending from a class other than Object , but the syntax is expected to change before appearing in all Dart SDKs. For details, see the mixin specification.

📝 Syntax

We saw how mixins can be useful, let’s see how to create and use them.

Mixins are implicitly defined via ordinary class declarations:

If we want to prevent our mixin to be instantiated or extended, we can define it like that:

To use a mixin, use the with keyword followed by one or more mixin name:

Defining the Walker mixin on the Cat class, allows us to call the walk method but not the fly method (defined in Flyer ).

🔎 Details

I told you I found this concept somewhat difficult to understand, but so far it’s not so difficult, is it?

Well, can you tell what the output of the following program is 😵?

You can run this program on DartPad

Both, AB and BA classes extend the P class with A and B mixins but in a different order. All three A, B and P classes have a method called getMessage.

First, we call the getMessage method of the AB class, then the getMessage method of the BA class.

So, what do you think the resulting output will be?

I’m giving you five propositions:

A. It does not compile

B. BA

C. AB

D. BAAB

E. ABBA