...

/

Introduction to Mixins

Introduction to Mixins

This lesson introduces the concept of mixins in Dart. In addition, you will learn to create mixins.

Introduction

Mixins allow Dart code to be reusable across separate classes. It is more efficient to reuse common code from multiple classes that share common behaviors.

A mixin class contains methods used by other classes without being their parent. It allows us to use a class’ code without having to inherit from it. This is how mixins differ from interfaces and abstract classes.

Declaring Mixins

Mixins are declared using the keyword mixin like below:

mixin SharedBehavior {

}

We can understand this with the help of an example of different people with different occupations: artist, engineer, doctor, and sportsman or athlete.

Assume there are common behaviors between different people like sketching, reading, exercising, and boxing.

The above four types of people may share a combination ...