What are the key differences between Dart extends, implements, and with?
This tests your grasp of Dart's inheritance, interface, and mixin models. extends gives single inheritance; implements forces full reimplementation; with injects shared behavior.
WHAT THIS TESTS: This question probes whether you can navigate Dart's object model beyond Java-style class inheritance. Dart uses single inheritance but layers two other reuse mechanisms on top: every class exposes an implicit interface, and mixins provide behavior injection without subclassing. The interviewer wants to see if you know the mechanical differences and can pick the right tool for class design.
A GOOD ANSWER COVERS: First, extends means single inheritance. A class can only extend one superclass, inheriting its implementation and optionally overriding methods. Second, implements means conforming to an interface. In Dart, any class can be used as an interface, but the implementing class must redeclare and provide its own implementation for every public member. It does not reuse the parent class code. Third, with means applying a mixin. Mixins are a way to add reusable behavior to multiple classes that do not share a common ancestor, using the with keyword. A senior candidate should also note that mixins are declared with mixin, not class, though classes can sometimes act as mixins under specific constraints. Finally, a practical scenario ties them together.
COMMON WRONG ANSWERS: A red flag is saying that implements reuses implementation from the parent class. It does not; it only enforces a contract. Another mistake is claiming that with establishes an is-a inheritance relationship like extends. Mixins are about composing behavior, not subtyping. Candidates sometimes say Dart supports multiple inheritance through with, which is false; mixins are linearized and applied in order, but they are not multiple inheritance. Also, saying you can extend multiple classes shows a fundamental gap.
LIKELY FOLLOW-UPS: The interviewer may ask how mixin application ordering resolves conflicts when two mixins define the same method. They might also ask when to prefer an abstract class over an interface, or how to enforce that a mixin can only be applied to certain types by using the on keyword. Another follow-up is why Flutter widgets use extends for State and StatefulWidget but mixins for ticker or animation behavior.
ONE CONCRETE EXAMPLE: Imagine building a Flutter app with shareable UI behavior. You have a base Animal class with name and move methods, so Cat extends Animal to inherit movement logic. You also have a Drawable interface requiring a render method; both Cat and Tree implement Drawable, but each supplies its own render body because Drawable as an interface provides no shared code. Finally, several unrelated widgets need logging, so you write a mixin Logger { void log() => print(status); } and apply it with Logger to both your Cat repository and your NetworkService. This keeps the logging code in one place without forcing those classes into the same hierarchy.
Read the original → dart.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.