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 -

  1. class Human{  
  2.    //Overridden method  
  3.     void run()  
  4.    {  
  5.       print("Human is running");  
  6.    }  
  7. }  
  8. class Man extends Human{  
  9.    //Overriding method  
  10.     void run(){  
  11.       print("Boy is running");  
  12.    }  
  13. }  
  14. void main(){  
  15.       Man m = new Man();  
  16.       //This will call the child class version of run()  
  17.       m.run();  
  18. }  

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

  1. class College{  
  2.  // Declaring variables  
  3.            String name;  
  4.            int rollno;  
  5.   
  6. // Overriden Method  
  7. void stu_details(name,rollno){  
  8.          this.name = name;  
  9.          this.rollno = rollno;  
  10.            
  11.   
  12. }  
  13.   
  14. void display(){  
  15.          print("The student name:${name}");  
  16.          print("The student rollno: ${rollno}");  
  17.          print("The result is passed");  
  18.   
  19.       }  
  20.   
  21.  }  
  22.   
  23. class Student extends College{  
  24. // Overriding Method  
  25. void stu_details(name,rollno){  
  26.          this.name = name;  
  27.          this.rollno = rollno;  
  28.   
  29. }  
  30.   
  31. void show(){  
  32.          print("The student name:${name}");  
  33.          print("The student rollno: ${rollno}");  
  34.   
  35.          print("The result is failed");  
  36.   
  37. }  
  38. }  
  39.   
  40. void main(){  
  41. //Creating object of subclass  
  42. Student  st = new Student();  
  43. st.stu_details("Joseph",101);  
  44. st.show();  
  45.   
  46. // Creating object of superclass  
  47. College cg = new College();  
  48. cg.stu_details("Jason",102);  
  49. cg.display();  
  50. }  

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 -

  1. class Human{  
  2.    //Overridden method  
  3.     void run()  
  4.    {  
  5.       print("Human is running");  
  6.    }  
  7. }  
  8. class Man extends Human{  
  9.    //Overriding method  
  10.     void run(){   
  11.        // Accessing Parent class run() method in child class  
  12.        super.run();  
  13.       print("Boy is running");  
  14.    }  
  15. }  
  16. void main(){  
  17.       Man m = new Man();  
  18.       //This will call the child class version of eat()  
  19.       m.run();  
  20. }  

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.

  1. 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.
  2. The overriding method must be defined in the subclass, not in the same class.
  3. The static and final method cannot be inherited in the subclass as they are accessible in their own class
  4. The constructor of the superclass cannot be inherited in a subclass.
  5. A method that cannot be inherited, then it cannot be overridden

Comments