Dart: Abstract Classes & Implicit Interfaces
An abstract class is a blueprint that other classes must follow, but it can't be instantiated itself. Dart also lets any class act as an 'implicit interface,' forcing other classes to implement its public API without inheritance.
Why it exists
To enforce a common structure across related classes without allowing the creation of a generic, incomplete base object. Abstract classes provide a way to define a contract and share code, while implicit interfaces allow for API contracts without forcing an inheritance relationship.
The mental model
Think of an abstract class as a partially completed house blueprint. It defines required rooms (abstract methods) and might have pre-designed plumbing (concrete methods), but you can't live in the blueprint itself. You must create a concrete house that finishes all the rooms. An implicit interface is different. It's like taking a finished car and telling a factory, "Build a new car with the exact same public controls, but you design the engine and chassis yourself." You're only copying the public API, not the implementation.
How it works
An abstract class is declared with the abstract keyword. It can contain abstract methods (signatures without a body) and concrete methods (with a body). Subclasses use extends and must implement all inherited abstract methods. You cannot create an instance of an abstract class. An implicit interface is created from any regular class. Another class uses the implements keyword, which forces it to provide its own implementation for every public member of the "interface" class. No code is inherited.
When to use it
Use an abstract class to share implementation among several closely related classes. For example, an abstract Shape class could have a concrete color property and an abstract calculateArea() method for Circle and Square to implement. Use an implicit interface when you want a class to conform to an API contract without sharing implementation or forcing an inheritance hierarchy, like defining a Logger contract for different logger types.
When not to use it
Don't use an abstract class if you only need to define a contract with no shared code; a regular class used as an interface is better. The biggest footgun is using implements when you want to inherit behavior and code from a superclass. For that, you must use extends. implements provides zero implementation from the interface.
One canonical example
An abstract class Animal could define a concrete breathe() method that all animals share, and an abstract makeSound() method. A Dog class would then extend Animal and only need to provide its own implementation for makeSound(). For an implicit interface, you could have a regular class Speaker with an announce() method. A different class, SmartSpeaker, could implement Speaker, which forces it to provide its own version of the announce() method, guaranteeing it has the same API but without inheriting any code from Speaker.
Interview question
Which Dart feature is best suited for defining an API contract that multiple unrelated classes must adhere to, without forcing them to inherit any shared implementation?
- a.An abstract class with concrete methods, used with the 'extends' keyword.
- b.A regular class, used with the 'extends' keyword.
- c.An abstract class with only abstract methods, used with the 'extends' keyword.
- d.A regular class used as an implicit interface, implemented with the 'implements' keyword.Correct
Why? this is the answer
An implicit interface, created from any regular class and used with 'implements', forces a class to provide its own implementation for every public member, ensuring an API contract without inheriting code. While an abstract class can define a contract (Option C), the card advises against using it if no code is shared, preferring a regular class as an interface for pure contracts.
Just read this? Test yourself on what you have been reading.
Read the original → dart.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.
See open roles