title: When to use mixins and when to use interfaces in Dart?
body: <p>I'm very familiar with the concepts of interfaces and abstract classes, but not super familiar with the concepts of <em>mixins</em>. </p>
  
  <p>Right now, in Dart, every class <code>A</code> defines an implicit interface, which can be implemented by another class <code>B</code> by using the <code>implements</code> keyword. There's no explicit way of declaring interfaces as, for example, in Java, where an interface contains only unimplemented methods (and eventually static variables). In Dart, since interfaces are defined by classes, the methods of the interface <code>A</code> may actually already be implemented, but the class that implements <code>B</code> still needs to override these implementations.</p>
  
  <p>We can see that situation from the following piece of code:</p>
  
  <pre><code>class A {
    void m() {
      print("method m");
    }
  }
  
  // LINTER ERROR: Missing concrete implementation of A.m
  // Try implementing missing method or make B abstract.
  class B implements A {
  }
  </code></pre>
  
  <p>In Dart, a mixin is also defined via ordinary class declarations...</p>
  
  <blockquote>
    <p>... In principle, every class defines a mixin that can be extracted from it. However, in this proposal, a mixin may only be extracted from a class that has no declared constructors. This restriction avoids complications that arise due to the need to pass constructor parameters up the inheritance chain.</p>
  </blockquote>
  
  <p>A mixin is basically a class that can define both unimplemented or implemented methods. It's a way to add methods to another class without needing to logically use inheritance. In Dart, a mixin is applied to a super class, that is extended via "normal" inheritance, like in the following example:</p>
  
  <pre><code>class A {
    void m() {
      print("method m");
    }
  }
  
  class MyMixin {
    void f(){
      print("method f");
    }
  }
  
  class B extends A with MyMixin {
  }
  </code></pre>
  
  <p>In this case, we should note that <code>B</code> does not have to implement any further methods both of <code>A</code> and <code>MyMixin</code>.</p>
  
  <p>There's a clear distinction between <em>applying a mixin</em> to a class and inheriting from a class, at least in a language that supports only single-parent inheritance, since, in that case, we could apply many mixins to a class, but a class could just inherit from another class.</p>
  
  <p>There's also a clear distinction between implementing an interface and inheriting from a class. The class that implements an interface needs to mandatorily implement all methods defined by the interface.</p>
  
  <p>So, in summary, the concept of implementing an interface is more about establishing a contract with the class that implements the interface, and the concept of mixins (as the name suggests) is more about reusing code (without recurring to an inheritance hierarchy).</p>
  
  <p>When to use mixins and when to use interfaces in Dart? Are there some rules of thumb for at least special recurrent patterns when designing software where it would be better to define a mixin and applying it to a super class rather than making our class implement an interface? I would appreciate concrete examples of design decisions in a context where both interfaces and mixins could be used, but one is used over the other (for some reason).</p>