Lets take a real world example for understand what does abstract class mean in dart.
Lets assume there a building with X number of floors. The area and width each floor is same. That happens due to the base we created for the building.
Like wise in dart we can create a base class and extend it to other classes. Abstract classes comes in handy when we want to create a base class for other classes to extend and use the base class methods.
The process of hiding the implementation details and showing only functionality to the user is known as abstraction.
Familiar with the quiz? Play Quiz
Abstract classes are classes that contain one or more abstract methods. An abstract method is simply a method with a declaration but no implementation. Abstract classes cannot be instantiated, but they can be subclassed.
Abstraction of a class is done by using the abstract keyword. Here are things that will be different in abstract classes then normal classes:
Example
Lets create a base class for the building.
abstract class Building {
int floors;
int width;
int area =0;
Building(this.floors, this.width) {
area = floors * width;
}
}
Now we can extend the Building class to create a new class. We are extending because we can’t directly create an instance of the Building class.
class House extends Building {
House(int floors, int width) : super(floors, width);
}
Now we can create a new instance of the House class.
void main() {
House house = House(10, 20);
print(house.area);
}
Output
200