DEV Community

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

Posted on

2 1

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.

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay