In this tutorial you will learn about the Dart Method Overriding and its application with practical example.
Dart Method Overriding
Extending a super class allows its subclass to use methods defined in super class by simply creating objects of subclass. This way we are allowed to use inherited methods in subclass without having to define it again in subclass. However, there may be occasions when we want subclass objects to respond differently to the same method when it is invoked using subclass objects. This is possible by defining same method again in subclass with the same name, same arguments and same return type as in the same method in superclass. Now, when that method is called, the method defined in subclass is invoked and executed instead of the method defined in superclass.
Declaring a method in sub class which is already exists in its parent class is known as method overriding. In Method Overriding, the subclass provide its own implementation for the method which already exists in superclass. Here, method defined in super class is known as overridden method and the method in subclass is called overriding method. Now, call to the method from subclass object will always call the subclass version of the method. However, using super keyword you can call super method.
Example:-
Lets take a simple example to understand the concept of Method Overriding. We have two classes, a sub class SubClass and a super class SuperClass. The SubClass extends SuperClass. Both the classes have a common method void display() with different implementation. SubClass class is giving its own implementation to the display() method or in other words it is overriding the display() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class SuperClass { //Overridden method void display(){ print("Parent class method"); } } class SubClass extends SuperClass { //Overriding method void display(){ print("Child class method"); } } void main(){ SuperClass supObj = new SuperClass(); SubClass subObj = new SubClass(); print("W3Adda - Method Overriding Example."); supObj.display(); subObj.display(); } |
When we run the above Dart Program, we will see the following output –
Output:-
Advantage Of Method Overriding
The main advantage of method overriding is that the individual subclass can provide its own implementation to a inherited method as per the requirement; without even modifying the super class method. This is helpful when we want a subclass objects to respond differently to the same method when it is invoked using subclass objects.
Rules for Method Overriding
1.) A method can only be written in Subclass, not in same class.
2.) The argument list should be exactly the same as that of the overridden method.
3.) The return type should be the same as declared in the original overridden method in the super class.
4.) A method declared final cannot be overridden.
5.) A method declared static cannot be overridden.
6.) If a method cannot be inherited then it cannot be overridden.
7.) Constructors cannot be overridden.