What is Abstraction?
Abstraction is one of the fundamental principles of object‑oriented programming. It means hiding the complex implementation details and showing only the essential features of an object. In Dart, abstraction is achieved using abstract classes and interfaces. Abstraction helps in reducing complexity and isolates the impact of changes.
Abstract Classes
An abstract class is a class that cannot be instantiated directly. It is declared using the abstract keyword. Abstract classes may contain both abstract methods (without a body) and concrete methods (with an implementation). They serve as blueprints for other classes.
Abstract Methods
An abstract method is a method declared without an implementation. It must be overridden by any non‑abstract subclass. Abstract methods can only exist inside abstract classes.
Interfaces (Implicit)
In Dart, every class implicitly defines an interface containing all its instance members. A class can implement one or more interfaces using the implements keyword. When a class implements an interface, it must provide concrete implementations of all members of that interface (even if they were already implemented in the interface class).
Unlike extends, implements does not inherit any implementation – you must provide everything.
Abstract Classes vs Interfaces
Both abstract classes and interfaces help define contracts, but they have differences:
- An abstract class can have both abstract and concrete methods, and can have fields. It is used when you want to share code among several closely related classes.
- An interface (via
implements) can only declare methods (and getters/setters) but provides no implementation. A class can implement multiple interfaces.
- An interface (via
- In practice, you often use abstract classes as base classes and interfaces to define capabilities.
Using implements with Multiple Interfaces
Dart allows a class to implement multiple interfaces, which is useful for mixing in different capabilities.
Abstract Classes with Factory Constructors
Sometimes you need to control object creation. Abstract classes can have factory constructors to return instances of concrete subclasses (e.g., based on some input).
Common Mistakes
- Trying to instantiate an abstract class (compile error).
- Forgetting to override an abstract method in a concrete subclass (compile error).
- Using
implementswhen you meantextends– losing code reuse.
- Using
- Making a class abstract unnecessarily.
Complete Example
Key Takeaways
- Abstraction hides implementation details and exposes only essential features.
- Abstract classes (declared with
abstract) cannot be instantiated and may contain abstract methods.
- Abstract classes (declared with
- -Abstract methods have no body and must be overridden in concrete subclasses.
- Every class defines an implicit interface; use
implementsto enforce that interface.
- Every class defines an implicit interface; use
- A class can implement multiple interfaces, but can extend only one class.
- Use abstract classes for code reuse among related classes; use interfaces to define capabilities.