Abstract Classes
In Dart, classes with one or more abstract methods are known as abstract classes. The portion of the data encapsulation known as abstraction is where the function’s true internal operations are concealed from users. They only communicate with features that are external. Using the abstract keyword, we can declare the abstract class. It is possible for an abstract class to have abstract methods and vice versa.
Methods that are declared but not implemented are called abstract methods. The conventional or concrete techniques are announced and put into practice. Both kinds of methods are permitted in abstract classes; however, normal classes are not permitted to have abstract methods.
An abstract class cannot be created since we are unable to generate its instance. Only the subclass has the ability to extend it, and the subclass needs to be given access to the abstract methods that are already included in the current class. After that, an abstract subclass needs to be declared.
Rules for Abstract classes:
The following list contains the abstract’s rules.
1.An abstract method, or method without implementation, may or may not be part of an abstract class.
2.The class needs to be declared abstract if there is at least one abstract method.
3.Although it cannot be created, the abstract class’s object can be extended.
4.The abstract class is declared using an abstract keyword.
5.It is also possible for regular or concrete (body-based) methods to be included in an abstract class.
6.The subclass is required to implement each and every abstract method of the parent class.
Declaring Abstract Class
To declare an abstract class, use the abstract keyword followed by the name of the class. An abstract class serves primarily as a foundation upon which a subclass might extend and implement an abstract method.
Syntax
abstract class ClassName {
// Body of abstract class
}
Usage of Abstract class
Assume for the moment that we have to create subclasses of Person called Boy and Girl in order to use the function displayInfo(). Implementing displayInfo() in the parent class is not beneficial because each person’s information is different from another’s. Because each subclass has to supply its own implementation of the parent class method, it must override it. That is why it is advantageous to make a method abstract: we may compel the subclass to implement it. The provide implementation in the parent class is not necessary.
Example
abstract class Person {
//declaring abstract method
void displayInfo(); //abstract method
}
class Boy extends Person
{
// Overriding method
void displayInfo() {
print("My name is Johnathon");
}
}
class Girl extends Person
{
// Overriding method
void displayInfo() {
print("My name is Grecia");
}
}
void main() {
Boy b = new Boy(); // Creating Object of Boy class
Girl g = new Girl(); // Creating Object of Girl class
b.displayInfo();
g.displayInfo();
}
Output
My name is Johnathon
My name is Grecia
Explanation
The code above illustrates how we implemented the abstract method in two subclasses in accordance with its requirements. We then used the objects of both classes to invoke the displayInfo() function.