Dart Inheritance and Polymorphism
Inheritance is one of the three major features of object-oriented programming; it allows a class to extend another class.
This chapter introduces single inheritance with extends, method overriding, the super keyword, and abstract classes in Dart.
Single Inheritance with extends
Dart only supports single inheritance -- each class can have only one direct parent class.
But the inheritance chain can be long: a subclass inherits its parent, the parent inherits its grandparent, and so on, passing down level by level.
Example
// Parent class (base class, superclass)
class
Animal
{
String name
;
Animal
(
this
.
name
)
;
void
eat
(
)
{
(
'$name is eating'
)
;
}
void
sleep
(
)
{
(
'$name is sleeping'
)
;
}
}
// Subclass: use extends to inherit from Animal
class
Dog
extends
Animal
{
String breed
;
// Breed
// The subclass constructor needs to call the parent constructor
Dog
(
String name
,
this
.
breed
)
:
super
(
name
)
;
// A method added by the subclass
void
bark
(
)
{
(
'$name ($breed) is barking!'
)
;
}
}
void
main
(
)
{
var
dog
=
Dog
(
'Wangcai' (a Chinese character)
,
'Jinmao' (a Chinese character)
)
;
// Call a method inherited from the parent class
dog.
eat
(
)
;
dog.
sleep
(
)
;
// Call the subclass's own method
dog.
bark
(
)
;
// A subclass object is also of the parent type
Animal animal
=
dog
;
// Dog is an Animal, so it can be upcast
animal.
eat
(
)
;
}
旺财 在吃东西
旺财 在睡觉
旺财(金毛)在汪汪叫!
旺财 在吃东西
A subclass can access all non-private member variables and methods of the parent class.
Private members (those starting with an underscore) are not inherited.
Dart has no public, protected, or private keywords. Members starting with an underscore (_) are library-private and can only be accessed within the same file. This differs from access control in Java/C++.
Method Overriding with @override
A subclass can override a parent class's method and provide its own implementation.
Using the @override annotation explicitly signals the intent to override, and the compiler will check that you have overridden correctly.
Example
class
Animal
{
String name
;
Animal
(
this
.
name
)
;
// The parent class's method
void
makeSound
(
)
{
(
'$name made some sound'
)
;
}
@override
String toString
(
)
{
return
'Animal: $name'
;
}
}
class
Cat
extends
Animal
{
Cat
(
String name
)
:
super
(
name
)
;
// @override annotation: tells the compiler I am overriding a parent class method
@override
void
makeSound
(
)
{
(
'$name meows~'
)
;
}
}
class
Duck
extends
Animal
{
Duck
(
String name
)
:
super
(
name
)
;
@override
void
makeSound
(
)
{
(
'$name quacks!'
)
;
}
}
void
main
(
)
{
var
cat
=
Cat
(
'Xiaohua' (a Chinese character)
)
;
var
duck
=
Duck
(
'Tanglaoya' (a Chinese character)
)
;
cat.
makeSound
(
)
;
duck.
makeSound
(
)
;
// Polymorphism: the same method behaves differently for different objects
List
<
Animal
animals
=
[
Cat
('Mimi' (a Chinese character)
)
,
Duck
(
'Yaya' (a Chinese character)
)
,
Cat
(
'Qiuqiu' (a Chinese character)
)
,
]
;
(
'--- RUNOOB Zoo ---'
)
;
for
(
var
animal
in
animals
)
{
animal.
makeSound
(
)
;
// At runtime, the method of the actual type is called
}
}
小花 喵喵叫~
唐老鸭 嘎嘎叫!
--- RUNOOB Zoo ---
咪咪 喵喵叫~
鸭鸭 嘎嘎叫!
球球 喵喵叫~
The example above demonstrates polymorphism: for the same method call animal.makeSound(), the behavior actually executed depends on the object's real type.
Using the @override annotation has two benefits: first, it makes the code's intent clearer; second, if the parent class has no method with the same name (for example due to a typo), the compiler reports an error to remind you. It is recommended to always add @override.
The super Keyword
super represents the parent class object; a subclass uses super to call the parent class's constructors, methods, and properties.
Example
class
Vehicle
{
String brand
;
int
year
;
Vehicle
(
this
.
brand
,
this
.
year
)
;
void
start
(
)
{
(
'$brand vehicle started'
)
;
}
void
displayInfo
(
)
{
(
'Brand: $brand, Year: $year'
)
;
}
}
class
ElectricCar
extends
Vehicle
{
int
batteryLevel
;
ElectricCar
(
String brand
,
int
year
,
this
.
batteryLevel
)
:
super
(
brand
,
year
)
;
// Call the parent class constructor
@override
void
start
(
)
{
// Call the parent class's method inside a subclass method
super
.
start
(
)
;
// First execute the parent class's startup logic
(
'Battery level: $batteryLevel%'
)
;
(
'Electric motor started silently'
)
;
}
@override
void
displayInfo
(
)
{
super
.
displayInfo
(
)
;
// First show the parent class's basic information
(
'Battery level: $batteryLevel%'
)
;
// Then add the subclass's specific information
}
}
void
main
(
)
{
var
car
=
ElectricCar
(
'RUNOOB EV'
,
2026
,
85
)
;
car.
start
(
)
;
(
'---'
)
;
car.
displayInfo
(
)
;
}
RUNOOB EV 车辆启动了
电池电量: 85%
电动机无声启动完成
---
品牌: RUNOOB EV, 年份: 2026
电池电量: 85%
Summary of super Use Cases
| Scenario | Syntax | Description |
|---|---|---|
| Call the parent class constructor | : super(args) | Must be called in the initializer list |
| Call the parent class method | super.methodName() | Can be called inside an overridden subclass method |
| Access parent class properties | super.propertyName | Access the parent class's member variables |
Abstract Classes with abstract
An abstract class is a class that cannot be instantiated directly; it only defines an interface specification, with subclasses implementing the concrete logic.
Example
// The abstract keyword declares an abstract class
abstract
class
Shape
{
// Abstract method: has no method body; subclasses must implement it
double
getArea
(
)
;
double
getPerimeter
(
)
;
// An abstract class can also have concrete methods
void
describe
(
)
{
(
'This is a shape, area: ${getArea()}, perimeter: ${getPerimeter()}'
)
;
}
}
class
Rectangle
extends
Shape
{
double
width
;
double
height
;
Rectangle
(
this
.
width
,
this
.
height
)
;
@override
double
getArea
(
)
=>
width
height
;
@override
double
getPerimeter
(
)
=>
2
(
width
height
)
;
}
class
Circle
extends
Shape
{
double
radius
;
Circle
(
this
.
radius
)
;
@override
double
getArea
(
)
=>
3.14159
radius
radius
;
@override
double
getPerimeter
(
)
=>
2
3.14159
radius
;
}
void
main
(
)
{
// Shape shape = Shape(); // Error! An abstract class cannot be instantiated
var
rect
=
Rectangle
(
10
,
5
)
;
var
circle
=
Circle
(
7
)
;
rect.
describe
(
)
;
circle.
describe
(
)
;
// Use an abstract class as a type
List
<
Shape
shapes
=
[
Rectangle
(
3
,
4
)
,
Circle
(
5
)
,
Rectangle
(
6
,
2
)
,
];
(
'--- RUNOOB Shape Statistics ---'
)
;
double
totalArea
=
0
;
for
(
var
shape
in
shapes
)
{
totalArea
=
shape.
getArea
(
)
;
}
(
'Total area: ${totalArea.toStringAsFixed(2)}'
)
;
}
这是一个形状,面积: 50.0, 周长: 30.0
这是一个形状,面积: 153.93791, 周长: 43.98226
--- RUNOOB Shape Statistics ---
总面积: 108.54
The core value of an abstract class is "defining a contract". It tells all subclasses: you must implement these methods, or the code will not compile. This makes team collaboration safer -- implementers will not miss methods, and callers can be sure the methods exist.