Dart Method Overriding

Summary: in this tutorial, you will learn about method overriding in Dart to override a method in a parent class from the same method in a child class.

Introduction to the Dart method overriding

Method overriding allows a method in a child class to override the same method in the parent class.

In Dart, all the classes inherit from the Object class. The Object class has some useful methods for example toString().

If you pass an object to the print() function, the print() function will call the toString() method of the object to get the string representation for display.

By default, the toString() method of the Object class returns a very generic string like this:

Instance of 'ClassName'Code language: Dart (dart)

To customize the return string, you need to override the toString method in your class. For example:

First, define a BankAccount class:

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)

Second, create a new instance of the BankAccount class and pass it to the print() function:

void main() {
  var account = BankAccount(balance: 100);
  print(account);
}Code language: Dart (dart)

Output:

Instance of 'BankAccount'Code language: Dart (dart)

Third, add the toString() method to the BankAccount class to make the string representation of its objects:

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;
  }

  @override
  String toString() {
    return 'The balance is $balance USD.';
  }
}Code language: Dart (dart)

The @override is an annotation that indicates the toString() method overrides the toString() method of the Object class.

The toString() method in the BankAccount class is an overridden method. It overrides the toString() method of the Object class.

Finally, run the program again:

void main() {
  var account = BankAcount(balance: 100);
  print(account);
}Code language: Dart (dart)

You’ll see the string representation of the BankAccount object:

The balance is 100.0 USD.Code language: Dart (dart)

Calling super in an overridden method

Sometimes, you want to add functionality to the method of the parent method rather than replace it. To do that, you can use the super keyword to call the method of the parent class:

super.parentMethod(parameters);Code language: Dart (dart)

For example, the following defines the SavingAccount class that inherits from 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;
  }

  @override
  String toString() {
    return super.toString() + ' The interest rate is ${interestRate}.';
  }
}Code language: Dart (dart)

In the SavingAcount class, we override the toString() method from the BankAccount class. However, instead of replacing the toString() method from the BankAccount completely, we reuse it by concatenating its result with another string.

The following uses the super keyword to call the toString() method of the BankAccount class from the toString() method of the SavingAccount class:

super.toString()Code language: Dart (dart)

Putting 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;
  }

  @override
  String toString() {
    return 'The balance is $balance USD.';
  }
}

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;
  }

  @override
  String toString() {
    return super.toString() + ' The interest rate is ${interestRate}.';
  }
}

void main() {
  var account = BankAccount(balance: 100);
  print(account);
}Code language: Dart (dart)

Summary

  • The overriding method allows a method in a child class to override the same method in the parent class.
  • Use the super keyword to call the same method from the parent class.
Was this tutorial helpful ?