Dart Abstract Classes
Dart Abstract Classes
An abstract class is a class that you cannot create objects from.
It is used as a superclass for other classes.
You create an abstract class with the abstract keyword before class:
Example
abstract class Animal {
String name;
Animal(this.name);
void sleep() {
print('$name is sleeping');
}
}
class Cat extends Animal {
Cat(super.name);
}
void main() {
var cat = Cat('Tom');
cat.sleep();
}
Try it Yourself »
Result:
Tom is sleeping
You Cannot Create an Object
If you try to create an object from an abstract class, you get an error:
Example
abstract class Animal {
void sleep() {
print('Sleeping');
}
}
void main() {
var animal = Animal();
animal.sleep();
}
Try it Yourself »
Result:
main.dart:8:16: Error: The class 'Animal' is abstract and can't be instantiated.
var animal = Animal();
^^^^^^
This is the point of an abstract class.
A general idea like "an animal" or "a shape" is not a real thing on its own.
Only the subclasses, like Cat or Circle, are real things.
Abstract Methods
An abstract method is a method without a body.
You write the method name and its parameters, and end it with a semicolon ; instead of curly brackets.
A normal class cannot have abstract methods, only an abstract class can.
Every subclass must override all abstract methods:
Example
abstract class Shape {
double area();
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double area() => 3.14 * radius * radius;
}
class Square extends Shape {
double side;
Square(this.side);
@override
double area() => side * side;
}
void main() {
print(Circle(2).area());
print(Square(3).area());
}
Try it Yourself »
Result:
12.56
9.0
If a subclass does not override an abstract method, you get an error:
Example
abstract class Shape {
double area();
}
class Triangle extends Shape {
double base = 4;
double height = 3;
}
void main() {
print(Triangle().base);
}
Try it Yourself »
Result:
main.dart:5:7: Error: The non-abstract class 'Triangle' is missing implementations for these members:
- Shape.area
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.
class Triangle extends Shape {
^^^^^^^^
main.dart:2:10: Context: 'Shape.area' is defined here.
double area();
^^^^
Abstract and Normal Methods Together
An abstract class can have both abstract methods and normal methods.
A normal method can even call an abstract method.
Each subclass gives its own version of the abstract method:
Example
abstract class Employee {
String name;
Employee(this.name);
double salary();
void printPay() {
print('$name earns \$${salary()} per month');
}
}
class Manager extends Employee {
Manager(super.name);
@override
double salary() => 5000;
}
class Intern extends Employee {
Intern(super.name);
@override
double salary() => 1200;
}
void main() {
Manager('Sara').printPay();
Intern('Ben').printPay();
}
Try it Yourself »
Result:
Sara earns $5000.0 per month
Ben earns $1200.0 per month
A List of Different Objects
You can use the abstract class as a type.
A list of type List<Shape> can hold circles, squares and any other shape:
Example
abstract class Shape {
String get name;
double area();
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
String get name => 'Circle';
@override
double area() => 3.14 * radius * radius;
}
class Square extends Shape {
double side;
Square(this.side);
@override
String get name => 'Square';
@override
double area() => side * side;
}
void main() {
List<Shape> shapes = [Circle(1), Square(2), Circle(2)];
for (var shape in shapes) {
print('${shape.name}: ${shape.area()}');
}
}
Try it Yourself »
Result:
Circle: 3.14
Square: 4.0
Circle: 12.56
As you can see, a getter can also be abstract.
Remember: Use an abstract class when several classes share the same idea, but each class does some of the work in its own way.