Dart Abstract Classes
Dart Abstract Classes
Dart abstract classes are classes for which we cannot create objects for the classes. In other words, an abstract class can’t be instantiated.
We can declare an abstract class using the abstract keyword. An abstract keyword followed by a class name is used to declare a class as an abstract class.
The general syntax to declare an abstract class is as follows:
abstract class ClassName {
// class body
}
Abstract classes are classes that may have one or more abstract methods. Abstract methods are methods, which are just declared without the actual implementation. On the other hand, concrete methods are methods that are declared with the actual implementation. An abstract class can contain both abstract and concrete methods.
Example
/*
Abstract class demo example
Dart Tutorials – www.TestingDocs.com
*/
// abstract class Account
abstract class Account {
// abstract method
void display();
}
class SavingAccount extends Account {
// concrete method
void display() {
print(“Savings Account”);
}
}
void main() {
//We cannot create an object
Account a = new Account();
}
The Account class is defined as an abstract class. The display() method is an abstract method without implementation or method body. We can see that we cannot instantiate the abstract Account class.
An abstract class is used to allow the subclass to extend and implement the abstract methods. We can make the class as abstract when we do not know the actual implementation of the methods during the class definition.
—
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/
More information on Dart: