About Dart Mixins

sKiridharan
4 min readMay 4, 2023

--

source : official blog

Foundation for Mixins

Recalling Inheritance and Composition

Definition of inheritance: Inheritance means inheriting/using the methods and functions of the parent class in the child class.

Definition of composition: Composition is the relation between different classes where the classes need not be the parent and child class. It represents a has a relation. The composition represents a strong relationship. It means if class A has Class B, then Class B can’t exist without class A.

How inheritance differs from composition: Inheritance defines the is a relationship where the child class is a type of parent class. Composition defines a part of relationship where the child is a part of the parent. Here’s an example:

Car is an automobile; it’s an example of inheritance.
Heart is part of human; it’s an example of composition.

Inheritance offers strong coupling, making it difficult to change or add functionality in the parent classes, whereas composition offers loose coupling. You might wonder why you would still use inheritance when you can use composition. The answer is that both have benefits and use cases, like how different fishing rods have their use cases.

Multiple inheritances in Flutter: Flutter doesn’t support multiple inheritances like some programming languages, such as C++. But there are ways to get the functionality of multiple inheritances in Flutter.

Now, you have an image of the differences between inheritance and composition. In the next section, you’ll learn about mixins.

1. What are Dart Mixins?

Mixins are a way to share functionality between classes. They add functionality without modifying the class that uses them, and they can be used with both inheritance and composition.
Mixins differ from traditional inheritance in that they don’t create an is-a relationship between two classes. Instead, they’re used to extend one or more existing classes with new methods and fields.

2. How do Dart Mixins Work?

Mixins are a way to reuse code and make it easier to maintain. They’re similar to classes, but they don’t have their own constructor or instance variables. Instead, you can use them as part of another class definition.

following mixin code snippet will help you understand this better:

Using Without Mixin Class:

class WithoutMixin{
void run(){
print("Hey, Running This without Mixin");
}
}

class SomeClass extends WithoutMixin{

}

void main(){
SomeClass obj=SomeClass();
obj.run();
}f

In the snippet above, you inherit WithoutMixin, which is inherited in SomeClass using the extends keyword. So the SomeClass is the child class of WithoutMixin.

Using Mixin Class:

mixin MixinClass{
void run(){
print("Hey, running!! With Mixin");
}
}

class SomeClass with MixinClass{}

void main(){
SomeClass someClassObj=SomeClass();
someClassObj.run();
}

In the snippet above, you use MixinClass in SomeClass using the with keyword. The SomeClass isn’t the child class. You need to use the with keyword to implement mixins.

Ok!!!!!

Are you getting confused between the with, implements and extends keywords available in Dart? No worries. The following section will clarify your understanding.

Extends keyword:

  • The extends keyword is commonly used in Dart to change the functionality of a class using inheritance.
  • Inheritance refers to a class’s ability to derive properties and attributes from another class. A class that inherits from another class is known as the subclass or derived class. The class whose properties and attributes are inherited is known as the parent class or superclass .

Implements keyword:

  • The implements keyword is used to force functions to be redefined in order to implement an interface.
  • An interface defines methods but does not implement them. Interface declaration does not have a syntax in Dart. Dart considers class definitions to be interfaces. To use an interface, classes need to use the implements keyword.
  • It forces us to override all methods of the class it implements.

With keyword:

  • Used to associate mixin classes with other classes.
  • Every method of the mixin class is imported to the other class.
  • Multiple mixin classes can be used with the same class.

Example For mixin:

// mixin with name ' teacher '
mixin teacher {
void func( ) {
print( ' This is the function of the mixin teacher ' ) ;
}
}
// mixin with name ' student '
mixin student {
void func2( ){
print( 125 ) ;
}
}
// mixin type used with keyword
class Principal with teacher, student{
@override
void func( ) {
print( ' We can override function inside this class if needed ' ) ;
}
}
void main( ) {
var princi = Principal( ) ;
princi.func( ) ;
princi.func2( ) ;
}

Learn More About Dart : https://dart.dev/language

--

--

sKiridharan

App developer and web development with django python . Learn by reading Practice by coding😍