Summary: in this tutorial, you’ll learn about the Dart super
keyword to reference the parent class from a child class.
Introduction to Dart super keyword
Unlike other OOP languages such as C#, in Dart, a subclass doesn’t inherit constructors from its parent class.
For example, the following adds a constructor to the BankAccount
class defined in the inheritance tutorial:
class BankAccount {
double _balance = 0;
BankAccount({double balance = 0}) : _balance = balance;
double get balance => _balance;
deposit(double amount) {
_balance += amount;
}
bool withdraw(double amount) {
if (amount <= _balance) {
_balance -= amount;
return true;
}
return false;
}
}
Code language: Dart (dart)
And the SavingAccount
class, which inherits from the BankAcount class, doesn’t have the unnamed constructor of the BankAccount
class:
class SavingAccount extends BankAccount {
double _interestRate = 0;
double get interestRate => _interestRate;
set interestRate(double value) {
if (value > 0) {
_interestRate = value;
}
}
Code language: Dart (dart)
To add an unnamed constructor to the SavingAccount
class, you need to explicitly define it in the class.
Typically, the constructor of the child class has more parameters than the constructor of the parent class. Also, you can use the super
keyword to call the constructor of the parent class to initialize the properties from the parent class.
For example, the following defines a constructor in the SavingAccount
and uses the super
keyword to initialize the properties of the BankAccount
class:
class SavingAccount extends BankAccount {
double _interestRate = 0;
SavingAccount({double balance = 0, double interestRate = 0})
: _interestRate = interestRate,
super(balance: balance);
double get interestRate => _interestRate;
set interestRate(double value) {
if (value > 0) {
_interestRate = value;
}
}
addInterest() {
double interest = _balance * _interestRate;
this._balance += interest;
}
}
Code language: Dart (dart)
In the constructor of the SavingAccount class, we initialize the _interestRate
field and use the super
to initialize the balance
property. Because the constructor uses an initializer list, the super
must appear last in the list:
SavingAccount({double balance = 0, double interestRate = 0})
: _interestRate = interestRate,
super(balance: balance);
Code language: Dart (dart)
The following creates a new instance of the SavingAccount
class and calls its constructor to initialize the balance
and interestRate
properties:
void main() {
var account = SavingAccount(balance: 1000, interestRate: 0.05);
account.addInterest();
print(account.balance);
}
Code language: Dart (dart)
Put it all together:
class BankAccount {
double _balance = 0;
BankAccount({double balance = 0}) : _balance = balance;
double get balance => _balance;
deposit(double amount) {
_balance += amount;
}
bool withdraw(double amount) {
if (amount <= _balance) {
_balance -= amount;
return true;
}
return false;
}
}
class SavingAccount extends BankAccount {
double _interestRate = 0;
SavingAccount({double balance = 0, double interestRate = 0})
: _interestRate = interestRate,
super(balance: balance);
double get interestRate => _interestRate;
set interestRate(double value) {
if (value > 0) {
_interestRate = value;
}
}
addInterest() {
double interest = _balance * _interestRate;
this._balance += interest;
}
}
void main() {
var account = SavingAccount(balance: 1000, interestRate: 0.05);
account.addInterest();
print(account.balance);
}
Code language: Dart (dart)
Summary
- A child class doesn’t automatically inherit constructors from the parent class.
- Use the
super
keyword to reference the parent class.