Updated 7 December 2023
The definition of Abstraction in Dart is the same as in other programming languages. Its main goal is to handle complexity by hiding unnecessary details from the user.
In this article, we will explain the abstraction with the help of examples.
Now let’s explore more on abstraction.
Abstraction is one of the four main principles of objected oriented programming.
In simple terms, abstraction means hiding unnecessary details. Abstraction involves breaking down a complex system into smaller ones. For instance, it allows to create models or representations that capture the essential features while hiding the unnecessary details.
Abstraction plays an important role in code reusability and maintainability.
One of the primary methods to implement abstraction is through abstract classes.
An abstract class cannot be instantiated by itself. Consider it as a blueprint for other classes.
Every class inheriting from an abstract class must implement the methods of the parent abstract class.
1 2 3 4 5 6 7 8 9 10 11 12 |
abstract class Animals { void voice(); void eat(); } class Cat extends Animals { @override void voice() { print('sound'); } @override void eat() { print('eat'); } } |
In the above example, the Animals class is made abstract using the abstract keyword.
It defines two abstract methods voice() and eat(). After that, the Cat class extends Animal and implements those methods.
Dart also supports interfaces, which are another way to achieve abstraction. An interface defines a contract that a class must adhere to, specifying the methods and properties it must implement.
1 2 3 4 5 6 7 8 |
abstract class Fly { void fly(); } class Airplane implements Fly { @override void fly() { print('The bird is flying.'); } } |
In this example, the Fly abstract class represents an interface. After that, the Airplane class implements the Fly interface by providing the required fly() method.
Above all, we can use abstraction to organize code and encapsulate implementation details.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
abstract class DataSource { Future<List<dynamic>> fetchData(); } class RemoteDataSource implements DataSource { @override Future<List<dynamic>> fetchData() { } } class LocalDataSource implements DataSource { @override Future<List<dynamic>> fetchData() { } } |
In this example, the DataSource abstract class defines a contract for fetching data. The RemoteDataSource and LocalDataSource classes implement this contract and provide their respective implementations. This approach allows for code separation and modular design, making it easier to switch between different data sources without affecting the overall functionality.
Abstraction is a powerful technique in Dart that helps developers manage complexity, improve code organization, and enhance software design. With the ability to hide unnecessary details and focus on essential aspects, abstraction becomes an indispensable tool for building robust and scalable applications in Dart.
For more content please check
You may also check our Flutter app development page.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.