Dart Method Overriding
Overview
Let’s understand method overriding in Dart programming language. Method overriding is an important concept in OOPS. ( Object-Oriented Programming System)
Method Overriding
Method overriding is a concept where a subclass provides a specific implementation for a method that is already defined in its superclass. In other words, when we declare the same method in the child class, which is already defined in the superclass, this is known as method overriding. This OOPS concept allows a subclass to modify or extend the behavior of a method inherited from its superclass.
The method overriding is a technique to achieve polymorphism. This technique is useful when we want a subclass object to give different results when it invokes the overridden method.
Example
Let’s understand the method overriding concept with an example.
/*
Method overriding demo example
Dart Tutorials – www.TestingDocs.com
*/
class Account {
// Parent class overridden method
void display()
{
print(“Bank account”);
}
}
class SavingAccount extends Account {
// Method Overriding in child class
void display() {
print(“Saving Account”);
}
}
void main() {
Account a = new Account();
//This will invoke the parent class display method
a.display();
SavingAccount acc=new SavingAccount();
//This will invoke the child class display method
acc.display();
}
We have defined two classes; Account and SavingAccount. The SavingAccount subclass inherits the Account superclass.
The method display() is defined with the different implementations in both classes. The subclass overrides the display() method. It has its own definition of the display() method.
When a method is called on an object, the actual implementation that gets executed is determined at runtime based on the object’s type, making it a powerful feature for creating flexible and extensible code.
—
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/
More information on Dart: