169

I want to understand the difference between extends, implements and with. When do I use each keyword?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Keerti Purswani
  • 4,878
  • 3
  • 16
  • 29

3 Answers3

197

Extends:

Use extends to create a subclass, and super to refer to the superclass.

Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.

You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.


Implements:

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.

Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.

When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.


With (Mixins):

Mixins are a way of reusing a class’s code in multiple class hierarchies.

With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.

They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.

NiklasPor
  • 9,116
  • 1
  • 45
  • 47
  • So, `extends` can't be used for multiple inheritance? Could you please explain "Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements" more? What will happen if I extend SingleTickerProviderStateMixin. instead of using with? – Keerti Purswani Mar 22 '19 at 09:37
  • 29
    In other programming languages, you can not `implement` classes, instead you can only `implement` interfaces. An interfaces only contains function and variable definitions, but no implementation. In `Dart` every class automatically has an interface, which includes all definitions. Therefore you can also use `implements` with classes. – NiklasPor Mar 22 '19 at 09:41
  • 2
    And yes `extends` can only be used with a single class. – NiklasPor Mar 22 '19 at 09:42
  • 5
    "every class automatically has an interface" - so all the implementation inside a class gets ignored when I implement it from another class? – Keerti Purswani Mar 22 '19 at 09:49
  • @Niklas What would happen if I used `with` keyword with a class instead of a mixin? The compiler does NOT complain. – kosiara - Bartosz Kosarzycki Oct 01 '19 at 12:22
  • "To implement a mixin, create a class that extends Object and declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin keyword instead of class." from the linked documentation. Nothing special, you extend the functionality of the other class with the class from the mixin keyword. – NiklasPor Oct 01 '19 at 12:55
  • Your SingleTickerProviderState mixin is deprecated. Here is the new link: https://api.flutter.dev/flutter/widgets/SingleTickerProviderStateMixin-mixin.html – QuackQuack Apr 19 '23 at 06:14
3

extend can only be used with a single class at the time, BUT... you can easily extend a class which extends another class which extends another class which...! ;)

In fact, most Flutter widgets are already built like that.

Karolina Hagegård
  • 1,180
  • 5
  • 26
0

another definition that may help you got the point.. Use extends make class inherits (extends) his parent, where you extend(have) his properties, methods beside his own properties and methods, also you should validate that this class(child) commits it's parent class and doesn't break the restrictions of the parent .. where as

Use implements when you want the class just restrict to the abstract class properties but you have your own implementation for these properties.