Sub topic 12
Abstract Classes and Implicit Interfaces
This chapter assumes you know Dart variables, functions, control flow, null safety, and the basic idea of objects and methods. You will learn how to define a shared contract with an abstract class and how Dart lets every class act as an interface.
Abstract classes
An abstract class is a class intended to be used as a base type. You cannot create an instance of it directly. It can contain both:
- Abstract members, which declare required behavior without providing an implementation.
- Concrete members, which provide behavior that subclasses can reuse.
Use the abstract keyword before class:
abstract class Shape {
double get area;
String describe() {
return 'Shape with area ${area.toStringAsFixed(1)}';
}
}
class Circle extends Shape {
final double radius;
Circle(this.radius);
@override
double get area => 3.14159 * radius * radius;
}
void main() {
final circle = Circle(2);
print(circle.describe());
}Shape requires every concrete subclass to provide an area getter. Circle supplies that getter and inherits the describe method.
A concrete subclass must implement every abstract member it inherits. The @override annotation is not required, but it lets the analyzer check that the member really overrides or implements a declaration.
Every class provides an implicit interface
In Dart, a class defines an interface automatically. An interface is a contract describing the public members that another class must provide.
Use implements when you want to satisfy that contract without inheriting the class's implementation:
abstract class Shape {
double get area;
String describe() {
return 'Shape with area ${area.toStringAsFixed(1)}';
}
}
class LabelledShape implements Shape {
@override
final double area;
final String label;
LabelledShape(this.area, this.label);
@override
String describe() {
return '$label: area ${area.toStringAsFixed(1)}';
}
}
void main() {
final shape = LabelledShape(12, 'Panel');
print(shape.describe());
}LabelledShape must implement both area and describe. It does not inherit Shape.describe. The class also does not inherit fields, constructors, or private implementation details from Shape.
You can implement more than one interface:
class CombinedShape implements Shape, Comparable<CombinedShape> {
@override
final double area;
CombinedShape(this.area);
@override
String describe() => 'Combined area: $area';
@override
int compareTo(CombinedShape other) => area.compareTo(other.area);
}The implements clause is useful when classes share a public contract but should have different internal designs.
`extends` compared with `implements`
| Choice | Reuses implementation? | Must provide declared members? | Number allowed |
|---|---|---|---|
| `extends` | Yes | Abstract members only | One superclass |
| `implements` | No | All interface members | Multiple interfaces |
A class can extend one class and implement several interfaces. Choose extends when the subclass is a specialized form of the superclass and should reuse its behavior. Choose implements when the important relationship is a contract rather than shared implementation.
Exercise
What must a class provide when it uses implements Shape, even if Shape already contains concrete methods?
Show answer
It must provide every public member declared by Shape, including members that have concrete implementations. It inherits no implementation from Shape.