Introduction
In this blog, we will look into Dart Abstract Class. We'll look at how an abstract class is defined and how derived classes can override its properties. We can't construct an object out of an abstract class, but we can make one out of a non-abstract class that is derived from it.
If you are new to Dart and are not familiar with classes in Dart, you can check out this article.
Dart Abstract Classes
To state-certain fundamental properties, an abstract class is utilized. In Dart, you can't instantiate an abstract class. Making an abstract class object is not possible; attempting to do so would result in a compilation error.
Abstract Class Declaration
In Dart, the abstract keyword is used to create an abstract class.
abstract class className{
//body of the class
}
Example
We'll explore how to declare an abstract class and how to create a derived class from an abstract class in this example.
// Creating an abstract class
abstract class GradeSheet {
// abstract method
void total_marks();
}
// derived class
class Marks extends GradeSheet {
// class variables
var maths;
var english;
// method to change marks of maths subject
void setMathsMarks(var num) {
maths = num;
}
// method to change marks of english subject
void setEnglishMarks(var num) {
english = num;
}
// overriding the total_marks function to calculate the total
@override
void total_marks() {
var total = maths + english;
print("Total Marks are: " + total.toString());
}
}
main() {
// Making an object of marks class
Marks m1 = new Marks();
// setting marks of english
m1.setEnglishMarks(30);
// setting marks of maths
m1.setMathsMarks(50);
// printing the total marks
m1.total_marks();
}
Output:
Total Marks are: 80




