Dart Method Overriding
Dart Method Overriding
What is Polymorphism?
The polymorphism is a combination of the two Greek words poly, which means many and morph means morphing into different forms or shapes. Together, polymorphism means the same entity can be used in various forms. In the programming aspect, the same method can be used in different classes. This technique makes programming more intuitive and more accessible.
For example - We have Shape class to define the shape of the object. The shape can be the circle, rectangle, square, straight line, etc. So here the goal is common, but the approach is different.
The method overriding is a technique to achieve polymorphism. Sometimes, we want a subclass object to give different results for the same method when subclass object invokes it. This can be done by defining the same method again in subclass. The method has the same name, same arguments, and the same return type. When that method is called, the subclass's method is executed instead of the method defined in the superclass.
Method Overriding
When we declare the same method in the subclass, which is previously defined in the superclass is known as the method overriding. The subclass can define the same method by providing its own implementation, which is already exists in the superclass. The method in the superclass is called method overridden, and method in the subclass is called method overriding. Let's understand the method overriding in the following example.
Method Overriding Example
We define two classes; first, is a subclass called Human, and the second is a superclass Boy. The Boy subclass inherits the Human superclass. The same method void showInfo() in both classes is defined with the different implementation. The subclass has its own definition of the void showInfo(). Let's have a look at the following code snippet.
Example -
Output:
Boy is running
Explanation:
In the above example, we defined a method with the same name in both, subclass and superclass. The purpose of method overriding is to give the own implementation of subclass method. When we created the object of the Boy subclass, it executed the subclass method and printed the Man is running instead of Human is running.
If we create the object of a parent class, then it will be always invoked the parent class method.
Let's take another example where we create two Classes called College and Student with common method void student_details(). Let's have a look at the following code snippet.
Example - 2
Output:
The student name: Joseph The student rollno: 101 The result is failed The student name:Peter The student rollno: 102 The result is passed
Explanation:
In the above example, we create two classes - College as a parent class and Student as a child class. The method stu_details defined in both classes with the same parameters and same return types.
Now, the College superclass is inherited by the Student subclass and the stu_details() method is overridden in the subclass.
We created the object of Student and to invoked the stu_details() with suitable arguments. It executed the subclass method, and then it printed the result.
Same as we created the object of College superclass object invoked its methods and printed the different results.
Method Overriding using super Keyword
We can invoke the parent class method without creating its object. It can be done by using the super keyword in the subclass. The parent class data member can be accessed in the subclass by using the super keyword. Let's understand the following example.
Example -
Output:
Human is running Boy is running
Explanation:
In the above program, we accessed the Human class method in child class using the super keyword. Now, we don't need to instantiate the parent class. We only created the object of subclass, which invoked the run() method of child class and parent class.
Note - When we created the child class object and invoked the method, it executes the parent class (if accessed by super keyword) method first, then the child class method.
Advantage of method overriding
The main benefit of the method overriding is that the subclass can provide its own implementation to the same method as per requirement without making any changes in the superclass method. This technique is much when we want to subclass method to behave differently also with the same name.
Rules of Method overriding in Dart
The few rules of method overriding are given below. These points must be kept in mind while declaring the same method in subclass.
- The overriding method (the child class method) must be declared with the same configuration as the overridden method (the superclass method). The return type, list of arguments and its sequence must be the same as the parent class method.
- The overriding method must be defined in the subclass, not in the same class.
- The static and final method cannot be inherited in the subclass as they are accessible in their own class
- The constructor of the superclass cannot be inherited in a subclass.
- A method that cannot be inherited, then it cannot be overridden
Comments
Post a Comment