Dart Abstraction
Abstraction in Dart
Abstraction is one of the fundamental concepts of Object-Oriented Programming (OOP). It refers to the concept of hiding the complex implementation details of a class while exposing only the essential features to the outside world. The main goal of abstraction is to reduce complexity and allow the user to focus on interacting with objects at a high level without needing to know their inner workings.
In Dart, abstraction is achieved using:
- Abstract Classes: A class that cannot be instantiated directly and is meant to be subclassed. It can have abstract methods (without implementation) that must be implemented by subclasses.
- Interfaces: Dart classes implicitly act as interfaces and can be used to define the blueprint for other classes.
- Methods with no implementation: These methods are defined in the abstract class but must be implemented by concrete subclasses.
Key Points of Abstraction:
- Abstract class: Contains both fully implemented methods and abstract methods (methods without implementation).
- Abstract methods: Methods that are declared but not implemented in the abstract class. Subclasses must provide the implementation for these methods.
- Concrete class: A subclass that implements the abstract methods from the abstract class.
Example of Abstraction in Dart
Let's create an example where we define an abstract class Shape
with an abstract method area()
. We then create two concrete classes, Circle
and Rectangle
, that extend Shape
and implement the area()
method.
Explanation:
Abstract class
Shape
:- It contains an abstract method
area()
, which has no implementation. - It also contains a regular method
displayShape()
, which is fully implemented and can be used by subclasses.
- It contains an abstract method
Concrete classes
Circle
andRectangle
:- Both classes extend the
Shape
class and implement thearea()
method. - In
Circle
,area()
is implemented asπ * r^2
, while inRectangle
,area()
is implemented aswidth * height
.
- Both classes extend the
In the
main()
function:- We create instances of
Circle
andRectangle
and invoke thearea()
method to calculate the area for each shape.
- We create instances of
Output:
Benefits of Abstraction:
- Reduces Complexity: Users only interact with essential features of a class, avoiding unnecessary details.
- Improves Maintainability: Changes in the implementation of abstract methods in subclasses won't affect the code that uses the abstract class.
- Increases Flexibility: Abstraction allows you to define common interfaces and then provide different implementations for each subclass, making the code more flexible and adaptable to changes.
Key Points to Remember:
- Abstract classes cannot be instantiated directly. You can only create objects of concrete subclasses that implement the abstract methods.
- Abstract methods define a contract that the subclasses must adhere to by providing an implementation for those methods.
- Concrete classes are responsible for providing the implementation of the abstract methods.
Example of Multiple Classes with Abstraction:
If we wanted to extend this example further, we could add additional classes, such as a Triangle
, to further demonstrate the flexibility of abstraction.
Output:
Summary:
- Abstraction helps in hiding unnecessary implementation details and shows only the necessary features of an object.
- Abstract classes are the building blocks of abstraction in Dart, enabling a clean design by focusing on the essential functionality.
- Through abstract methods in abstract classes, you can enforce a contract that subclasses must follow, ensuring consistency while still allowing flexibility in implementation.
Abstraction is a great way to design your code in a more modular and maintainable manner, allowing you to build complex systems by focusing on high-level operations without worrying about the implementation details.