Dart/Flutter Compare abstract class, interface, mixin

Tram Ho

When programming dart and flutter you must have encountered and used abstract class, interface(implements class) and mixin but whether you already understand the difference between them, let’s compare to clarify the difference

I have the following example

extensions

When you extend, you will

  • Can only extensions 1 class
  • Must override abstract fun or C1 is abstract class
  • Reuse normal fun

implements

When you implements then you will

  • Can implements multiple classes
  • Must override again all fun, including normal fun but not reuse, or C2 is abstract class

mixin

mixin is a way of reusing dart’s code, taking advantage of and overcoming the disadvantages of extends and implements

When you with mixin then you will

  • Reusable code
  • Can with multiple mixin
  • You can restrict the class to use the mixin with on Class
  • Cannot extends mixin
  • If implements mixin then override again all fun or C3 is abstract class, not code reuse
  • Where with multiple mixin have the same fun, fun of mixin finally in the mixin list to be executed

In the above example, the parent class is an abstract class, if the parent class is a normal class, it’s the same, you guys can find out.

Share the news now

Source : Viblo