Dart Inheritance
Dart Inheritance
Inheritance means that one class gets the fields and methods of another class.
- Superclass (or parent class): the class that is inherited from
- Subclass (or child class): the class that inherits
To inherit from a class, use the extends keyword.
In this example, the Dog class inherits from the Animal class:
Example
class Animal {
String name = 'Animal';
void eat() {
print('$name is eating');
}
}
class Dog extends Animal {
void bark() {
print('Woof!');
}
}
void main() {
var dog = Dog();
dog.name = 'Rex';
dog.eat();
dog.bark();
}
Try it Yourself »
Result:
Rex is eating
Woof!
The Dog class has no name field and no eat() method of its own.
It gets them from Animal.
Note: A Dart class can only extend one class.
Override a Method
A subclass can write its own version of a method from the superclass.
This is called overriding the method.
Put @override above the method to show that it replaces a method from the superclass:
Example
class Animal {
void makeSound() {
print('The animal makes a sound');
}
}
class Cat extends Animal {
@override
void makeSound() {
print('Meow');
}
}
class Cow extends Animal {
@override
void makeSound() {
print('Moo');
}
}
void main() {
Animal().makeSound();
Cat().makeSound();
Cow().makeSound();
}
Try it Yourself »
Result:
The animal makes a sound
Meow
Moo
@override is an annotation, a note for Dart and for other programmers.
The code also runs without it, but it is good practice to always write it.
The tools can then warn you if the method name is wrong and nothing is overridden.
Call a Superclass Method
The super keyword refers to the superclass.
Use super.methodName() to call the superclass version of a method from inside the subclass.
This lets you add to a method instead of replacing it completely:
Example
class Vehicle {
void start() {
print('Engine started');
}
}
class Car extends Vehicle {
@override
void start() {
super.start();
print('Car is ready to drive');
}
}
void main() {
var car = Car();
car.start();
}
Try it Yourself »
Result:
Engine started
Car is ready to drive
Call the Superclass Constructor
If the superclass has a constructor with parameters, the subclass must call it.
You do this with : super(...) after the parameter list of the subclass constructor:
Example
class Person {
String name;
Person(this.name);
}
class Student extends Person {
String school;
Student(String name, this.school) : super(name);
}
void main() {
var student = Student('Emma', 'Oak High');
print(student.name);
print(student.school);
}
Try it Yourself »
Result:
Emma
Oak High
If you forget to call the superclass constructor, you get an error:
Example
class Person {
String name;
Person(this.name);
}
class Student extends Person {
String school;
Student(this.school);
}
void main() {
var student = Student('Oak High');
print(student.school);
}
Try it Yourself »
Result:
main.dart:10:3: Error: The implicitly called unnamed constructor from 'Person' has required parameters.
Try adding an explicit super initializer with the required arguments.
Student(this.school);
^
Super Parameters
Dart has a shorter way to pass a parameter to the superclass constructor.
Write super.name in the parameter list, in the same way as this.name.
This is called a super parameter:
Example
class Person {
String name;
int age;
Person(this.name, this.age);
void introduce() {
print('Hi, I am $name and I am $age years old.');
}
}
class Teacher extends Person {
String subject;
Teacher(super.name, super.age, this.subject);
@override
void introduce() {
super.introduce();
print('I teach $subject.');
}
}
void main() {
var teacher = Teacher('Olivia', 41, 'Math');
teacher.introduce();
}
Try it Yourself »
Result:
Hi, I am Olivia and I am 41 years old.
I teach Math.
Teacher(super.name, super.age, this.subject) does the same as Teacher(String name, int age, this.subject) : super(name, age).
Check the Type
An object of a subclass is also an object of its superclass.
A Dog is an Animal, so you can store it in a variable of type Animal:
Example
class Animal {
void makeSound() {
print('Some sound');
}
}
class Dog extends Animal {
@override
void makeSound() {
print('Woof');
}
}
void main() {
Animal pet = Dog();
pet.makeSound();
print(pet is Dog);
print(pet is Animal);
}
Try it Yourself »
Result:
Woof
true
true
The variable has the type Animal, but Dart runs the Dog version of the method, because the object is a Dog.