Welcome to this blog post, where we dive deep into the concept of the Abstract class in Flutter. Flutter, a ubiquitous and highly-used platform for crafting high-quality applications, is built on Dart. Dart, with its robust arsenal of Object-oriented programming (OOP) concepts, empowers developers with diverse tools. Our focus in this blog will be the Abstract class, a vital part of Dart's OOPs capabilities.
In Flutter development, coding structures play an essential part in molding reliable, readable, and scalable code. Abstract classes, a significant subset of these structures, provide developers with the means to uphold abstraction and encapsulation, two pillars of OOP.
Let's begin with understanding abstract classes at the fundamental level. These classes, a staple part of Object-oriented programming, are primarily utilised as blueprints. An Abstract class predefines a base structure for other classes to utilize and adapt in their definitions.
What sets the abstract class apart is its distinctive characteristic - it cannot be instantiated. Hence, one cannot directly create an object from an abstract class. Let's shade some light over the distinguishing features of abstract classes.
In detail, an Abstract class in Flutter is a class that cannot be used to instantiate objects. Sounds counter-intuitive, right? Don't worry, it will all make sense soon. An abstract class primarily serves two purposes; it is used to create interfaces and define partially implemented classes in Dart.
Declare a class as abstract by using the abstract keyword before the class name like this:
1abstract class Shape { 2 void draw(); // Abstract methods 3}
Here, void draw() is an abstract method that doesn't contain a method body; however, it forces the concrete classes or child classes derived from this abstract class to provide an implementation for this method.
A derived or concrete class will look like this:
1class Triangle extends Shape { // Class Triangle extends Shape 2 void draw() { // own implementation 3 print('Drawing Triangle'); 4 } 5}
This concrete class, Triangle, must provide an implementation for every abstract method declared in the parent Shape class, forming its own implementation.
In abstract classes, there are two categories that methods fall into: abstract methods and normal methods. An abstract method is a method declared in an abstract class but has no implementation within the abstract class itself.
Let's discuss specifics and bring the abstract to concrete with an example. In the following piece of code, we declare an abstract class named Cat. We declare several abstract methods, such as void walk(), for which we only provide the signatures.
1abstract class Cat { // Abstract class Cat 2 void walk(); 3 void talk(); 4}
Next up, we create a child class that extends the Cat base class, thus providing the required implementation for all the abstract methods.
1class PersianCat extends Cat { // Class Cat 2 void walk() { // Void walk 3 print('The Persian cat is walking...'); 4 } 5 6 void talk() { 7 print('The Persian cat says meow...'); 8 } 9} 10 11void main() { // void main 12 PersianCat cat = PersianCat(); 13 cat.walk(); 14 cat.talk(); 15}
Above code creates a new instance of the PersianCat and uses it to call the walk() and talk() methods.
The capability of using an abstract class as a blueprint lends greater assurance to developers. It ensures that child classes adhere to certain rules laid out by the parent class method.
This kind of design is highly beneficial when we work with complex systems, where multiple interfaces are to be integrated, or when a parent class is subject to frequent changes.
On bringing this deep dive on Abstract class in Flutter to a close, it becomes pretty apparent that understanding and effectively using abstract classes, concrete classes, and abstract and concrete methods could significantly elevate one's Flutter game. From fostering better class design to ensuring stricter adherence to class method implementation, the utility of abstract classes is certainly far-reaching.
Reliable Flutter apps are a product of not only solid logic but also mindful architecture. So here's hoping that this blog post equipped you with another tool to improve the structure of your Flutter apps and become more fluent in the language of Flutter.
Keep coding, keep learning!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.