Summary: in this tutorial, you’ll learn how to use Dart inheritance to define a class that reuses the methods and properties of another class.
Introduction to the Dart inheritance
Inheritance allows you to define a class that extends the functionality of another class.
Dart supports single inheritance. It means that a class can inherit from a single class. Dart doesn’t support multiple inheritances. In other words, a class cannot inherit from two or more classes.
To define a class that inherits from another class, you use the extends
keyword as follows:
class ChildClass extends ParentClass {
//...
}
Code language: Dart (dart)
In this syntax, a class that inherits from another class is called a child class, a subclass, or a derived class. A class from which the child class inherits is called a parent class, a super class, or a derived class.
The child class will have all properties and methods of the parent class. Also, it can extend the parent class by either overriding the methods from the parent class or having more methods in the child class.
Dart inheritance example
First, define the BankAccount
class that has one field _balance
, a getter balance
, and two methods deposit()
and withdraw()
:
class BankAcount {
double _balance = 0;
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)
Second, define a SavingAccount
class that inherits from the BankAcount
class:
class SavingAccount extends BankAcount {
}
Code language: Dart (dart)
Since the SavingAccount
class inherits from the BankAccount
class, it has all properties and methods of the BankAccount
class. And you can use the SavingAccount
class as the BankAccount
class.
Third, create a new instance of the SavingAcount
class, call the deposit()
and withdraw()
methods, and display the balance:
var account = BankAcount();
account.deposit(1000);
account.withdraw(200);
print(account.balance);
Code language: Dart (dart)
Put it all together:
class BankAcount {
double _balance = 0;
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 BankAcount {}
void main() {
var account = BankAcount();
account.deposit(1000);
account.withdraw(200);
print(account.balance);
}
Code language: Dart (dart)
Add more functions to a child class
First, add the _interestRate
field, interestRate
getter/setter and addInterest()
method to the SavingAccount
class:
class SavingAccount extends BankAcount {
double _interestRate = 0;
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)
Now, the SavingAccount
class has its own properties and the inherited properties from the BankAccount
class.
Second, create a new instance of the SavingAccount
, call the deposit()
method, add set the interest rate, add the interest to the balance, and display the balance:
void main() {
var account = SavingAccount();
account.deposit(1000);
account.interestRate = 0.05;
account.addInterest();
print(account.balance);
}
Code language: Dart (dart)
Output:
1050.0
Code language: Dart (dart)
Put it all together.
class BankAcount {
double _balance = 0;
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 BankAcount {
double _interestRate = 0;
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();
account.deposit(1000);
account.interestRate = 0.05;
account.addInterest();
print(account.balance);
}
Code language: Dart (dart)
The is operator
In practice, you use inheritance to model the is-a relationship. For example, a saving account is a bank account.
The is
operator returns true
if an object is an instance of a class or false
otherwise. The is
operator also returns true
if you test the object with the parent class of its class:
var account = SavingAccount();
print(account is SavingAccount); // true
print(account is BankAcount); // true
Code language: Dart (dart)
In this example, the account is an instance of the SavingAccount
class. Therefore, the following expression returns true
:
account is SavingAccount
Code language: Dart (dart)
Because the SavingAccount
is a child class of the BankAccount
class, the account
is also an instance of the BankAccount
class:
account is BankAcount
Code language: Dart (dart)
In Dart, all classes inherits from the Object
class. Therefore, all objects are instances of the Object
class.
Summary
- Inheritance allows a child class to inherit the properties and methods from another class.