Home > Net >  What is the difference between class and mixin in dart?
What is the difference between class and mixin in dart?

Time:05-07

What is the difference between:

class A {}

class B with A{}

and

mixin A{}

class B with A{}

?

CodePudding user response:

In Dart, a class can only extend one other class. But it can implement or mixin as many as you want. The thing is, when you extend a class, you inherit all its attributes, methods and it's constructors. When you implement a class, if you are adding only methods/attributes that you don't already have, you can simply continue your code. If you are implementing an abstract method, you'll need to actually implement it. Now, mixins are like extended classes, the classes that mix them, are their child as well as with extend and implements, but it has no constructor.

The actual idea for mixins is that you can add functionalities to any class, and they don't have to extend another class. That's why they usually do simple stuff only.

  • Related