Menu
Coddy logo textTech

Using Multiple Mixins

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 52 of 110.

Just like you can implement multiple interfaces, you can also apply multiple mixins to a single class. Simply list them after the with keyword, separated by commas.

mixin Swimming {
  void swim() => print('Swimming');
}

mixin Flying {
  void fly() => print('Flying');
}

mixin Walking {
  void walk() => print('Walking');
}

class Duck with Swimming, Flying, Walking {
  void quack() => print('Quack!');
}

void main() {
  var duck = Duck();
  duck.swim();   // From Swimming mixin
  duck.fly();    // From Flying mixin
  duck.walk();   // From Walking mixin
  duck.quack();  // From Duck class
}

The order of mixins matters when they have methods with the same name. The last mixin in the list wins:

mixin A {
  void greet() => print('Hello from A');
}

mixin B {
  void greet() => print('Hello from B');
}

class MyClass with A, B {}

void main() {
  MyClass().greet();  // Output: Hello from B
}

Since B comes after A, its greet() method takes precedence. This layering behavior lets you compose classes with exactly the capabilities you need, combining functionality from multiple sources without complex inheritance hierarchies.

challenge icon

Challenge

Easy

Let's build a smart home device system that combines multiple capabilities through mixins. You'll create different mixins representing various smart features, then build a device that uses all of them together.

You'll organize your code into two files:

  • smart_features.dart: Define three mixins that provide different smart home capabilities:
    • A VoiceControl mixin with a method voiceCommand(String command) that prints Voice: [command]
    • A WiFiEnabled mixin with a method connectWiFi(String network) that prints Connected to [network]
    • A PowerSaver mixin with a method enterSleepMode() that prints Entering sleep mode... and a method wakeUp() that prints Waking up!
    Then create a SmartSpeaker class that uses all three mixins. Add a String name property with a constructor, and include a method playMusic(String song) that prints [name] playing: [song]
  • main.dart: Import your smart features file and demonstrate how the speaker combines all capabilities:
    • Create a SmartSpeaker named 'Echo'
    • Call connectWiFi('HomeNetwork')
    • Call voiceCommand('Play jazz')
    • Call playMusic('Smooth Jazz Mix')
    • Call enterSleepMode()
    • Call wakeUp()

Your SmartSpeaker gains methods from all three mixins with a single with clause, demonstrating how multiple mixins let you compose exactly the capabilities you need without complex inheritance.

Expected output:

Connected to HomeNetwork
Voice: Play jazz
Echo playing: Smooth Jazz Mix
Entering sleep mode...
Waking up!

Cheat sheet

A class can apply multiple mixins by listing them after the with keyword, separated by commas:

mixin Swimming {
  void swim() => print('Swimming');
}

mixin Flying {
  void fly() => print('Flying');
}

class Duck with Swimming, Flying {
  void quack() => print('Quack!');
}

void main() {
  var duck = Duck();
  duck.swim();   // From Swimming mixin
  duck.fly();    // From Flying mixin
  duck.quack();  // From Duck class
}

When multiple mixins have methods with the same name, the last mixin in the list takes precedence:

mixin A {
  void greet() => print('Hello from A');
}

mixin B {
  void greet() => print('Hello from B');
}

class MyClass with A, B {}

void main() {
  MyClass().greet();  // Output: Hello from B
}

Try it yourself

import 'smart_features.dart';

void main() {
  // TODO: Create a SmartSpeaker named 'Echo'
  
  // TODO: Call connectWiFi('HomeNetwork')
  
  // TODO: Call voiceCommand('Play jazz')
  
  // TODO: Call playMusic('Smooth Jazz Mix')
  
  // TODO: Call enterSleepMode()
  
  // TODO: Call wakeUp()
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming