Sub topic 9
Inheritance, abstract classes, and mixins
This chapter assumes basic Dart syntax, classes, constructors, and object-oriented concepts. You will see how Dart reuses behavior through inheritance and mixins, and how abstract classes define contracts without being instantiated directly.
Inheritance
Inheritance creates a relationship between a general class and a more specific class. The specific class uses extends and receives the superclass's fields and methods.
A subclass can add new behavior or override an inherited method. An override replaces the inherited implementation for that subclass. Use @override so the analyzer can check that the method actually exists in the superclass.
Abstract classes
An abstract class describes behavior that its subclasses must provide. You cannot create an object directly from an abstract class. An abstract method has a declaration but no implementation.
abstract class Animal {
final String name;
Animal(this.name);
String sound();
void describe() {
print('$name makes a ${sound()} sound.');
}
}
class Dog extends Animal {
Dog(super.name);
@override
String sound() => 'Woof';
}
void main() {
final dog = Dog('Rex');
dog.describe();
}Animal stores the common name field and supplies describe. Its sound method is abstract, so every concrete subclass must implement it. Dog(super.name) forwards the constructor argument to the superclass constructor. Dart 3 calls this a super parameter.
An abstract class can contain both implemented and abstract methods. This lets it share common code while requiring subclasses to fill in the parts that differ.
Mixins
A mixin is a reusable group of methods and fields. A class applies a mixin with with. Unlike inheritance, a class can use several mixins.
mixin Walker {
void walk() {
print('$runtimeType walks');
}
}
abstract class Animal {
final String name;
Animal(this.name);
String sound();
}
class Dog extends Animal with Walker {
Dog(super.name);
@override
String sound() => 'Woof';
}
void main() {
final dog = Dog('Rex');
print('${dog.name}: ${dog.sound()}');
dog.walk();
}Dog inherits its identity and required sound behavior from Animal. It receives the walk method from Walker. The runtimeType expression reports the object's runtime class, so this program prints that Dog walks.
A mixin is useful when behavior cuts across an inheritance tree. For example, Walker could apply to animals, robots, or game characters. Use inheritance for an “is a” relationship and a mixin for reusable capability.
Common errors
| Error | Cause | Fix |
|---|---|---|
| Instantiating an abstract class | Abstract classes define contracts | Instantiate a concrete subclass |
| Missing an abstract method | A subclass did not complete the contract | Implement every abstract member |
| Using `extends` for unrelated behavior | Inheritance creates a strong type relationship | Consider a mixin instead |
| Forgetting `@override` | A method may be misspelled or have the wrong signature | Add `@override` and run the analyzer |
Exercise
What does Dog receive from Animal, and what does it receive from Walker in the example?
Show answer
Dog receives the name field and the required sound contract from Animal. It receives the walk method from Walker.