Method Overriding
What is Polymorphism?
The Greek words poly, which means many, and morph, which means morphing into different forms or shapes, are combined to generate the term polymorphism. When combined, polymorphism allows for the use of one entity in multiple configurations. Programming-wise, the same technique can be used to several classes. This method improves the accessibility and intuitiveness of programming.
For instance, the Shape class allows us to specify an object’s shape. The shape could be a square, rectangle, circle, or even a straight line. In this case, the objective is the same, but the strategy is not.
One method for achieving polymorphism is method overriding. Occasionally, we would like a subclass object to return distinct outcomes when another subclass object calls the same method. Redefining the same method in a subclass will do this. The method’s name, arguments, and return type are all the same. The method defined in the subclass is invoked when that method is called, not the method defined in the superclass.
Method Overriding
Method overriding is the process of declaring the same method that was previously specified in the superclass in the subclass. By supplying its own implementation of the method, which is already present in the superclass, the subclass can define the same method. The subclass method is referred to as method overriding, while the superclass method is termed method overridden. Let’s examine the method overriding in the example that follows.
Method Overriding Example
We define two classes, a superclass named Boy and a subclass called Human. The Human superclass is inherited by the Boy subclass. Both classes specify the same function, void showInfo(), but they have distinct implementations. The void showInfo() has a definition specific to the subclass. Let’s examine the subsequent sample of code.
Example
class Human{
//Overridden method
void run()
{
print("Human is running");
}
}
class Man extends Human{
//Overriding method
void run(){
print("Boy is running");
}
}
void main(){
Man m = new Man();
//This will call the child class version of run()
m.run();
}
Output
Boy is running
Explanation
We defined a method with the same name in both the superclass and subclass in the example above. Giving a subclass method an own implementation is the goal of method overriding. Upon creating the Boy subclass object, the subclass method was called, resulting in the output “Man is running” rather than “Human is running.”
The parent class method will always be called if we create an object of that class.
Using the standard function void student_details(), let’s construct two classes, College and Student, in this example. Let’s examine the subsequent sample of code.
Example
class College{
// Declaring variables
String name;
int rollno;
// Overriden Method
void stu_details(name,rollno){
this.name = name;
this.rollno = rollno;
}
void display(){
print("The student name:${name}");
print("The student rollno: ${rollno}");
print("The result is passed");
}
}
class Student extends College{
// Overriding Method
void stu_details(name,rollno){
this.name = name;
this.rollno = rollno;
}
void show(){
print("The student name:${name}");
print("The student rollno: ${rollno}");
print("The result is failed");
}
}
void main(){
//Creating object of subclass
Student st = new Student();
st.stu_details("Joseph",101);
st.show();
// Creating object of superclass
College cg = new College();
cg.stu_details("Jason",102);
cg.display();
}
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
We establish two classes in the example above: Student is a child class and College is the parent class. The identical parameters and return types are used by the stu_details method, which is defined in both classes.
The Student subclass now inherits the College superclass, and the subclass overrides the stu_details() method.
After creating the Student object, we called stu_details() with the appropriate inputs. It printed the outcome after carrying out the subclass method.
In the same way that we constructed the College superclass object, we called its methods and got various outputs.
Method Overriding using super Keyword
Without first generating the parent class object, we can call the method on it. The super keyword in the subclass can be used to accomplish this. The super keyword in the subclass can be used to access the parent class data member. Let’s examine the next illustration.
Example
class Human{
//Overridden method
void run()
{
print("Human is running");
}
}
class Man extends Human{
//Overriding method
void run(){
// Accessing Parent class run() method in child class
super.run();
print("Boy is running");
}
}
void main(){
Man m = new Man();
//This will call the child class version of eat()
m.run();
}
Output
Human is running
Boy is running
Explanation
The super keyword was used in the application mentioned above to access the Human class method in the child class. We can stop instantiating the parent class now. All that was produced was the subclass object that called the parent and child classes’ run() methods.
Note: Upon creating the child class object and calling the method, the parent class method (if accessible via the super keyword) is executed before to the child class method.
Advantage of method overriding
The primary advantage of method overriding is that, without altering the superclass method, the subclass can supply its own version of the same method in accordance with requirements. This method is quite useful when we wish to give a subclass function a different name but still operate differently.
Rules of Method overriding in Dart
Below are a few guidelines for method overriding. When declaring the same method in a subclass, some considerations must be made.
1.The configuration of the overridden method (the superclass method) and the overriding method (the child class method) must match when they are declared. The parent class method’s return type, parameter list, and order must all match.
2.Rather than in the same class, the overriding method needs to be defined in the subclass.
3.Since the static and final methods are available in their own class, they cannot be inherited by the subclass.
4.A subclass cannot inherit the superclass’s constructor.
5.If a technique is not inheritable, it cannot be overridden.