DEV Community

Cover image for In Dart: extends vs. with vs. implements
Amr Azzam
Amr Azzam

Posted on

In Dart: extends vs. with vs. implements

𝐞𝐱𝐭𝐞𝐧𝐝𝐬: Use extends to create a subclass that inherits the properties and methods of a superclass. The new class will inherit all the properties and methods of the superclass and can add additional properties and methods of its own. For example, if you have a superclass called Animal, you might create a subclass called Dog that extends Animal.

𝐰𝐢𝐭𝐡: Use with to mix in the behavior of a mixin into a class. A mixin is a class that provides a set of methods and properties that can be added to another class without creating a new subclass. This allows you to reuse code across multiple classes in a flexible way. For example, you might define a mixin called Logger that provides a log method, and then mix it into several classes that need logging behavior.

𝐢𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭𝐬: Use implements to declare that a class conforms to a certain interface. An interface is a set of methods and properties that a class must implement in order to satisfy the interface. This allows you to write code that works with any class that implements the interface, without needing to know the specifics of each class. For example, if you have an interface called Drawable that specifies a draw method, you might declare that a class called Rectangle implements Drawable.

Top comments (0)