We finished Aggregation in Java in the previous article and before that we discussed Inheritance in Java. These two topics in Java are enough to set the background for polymorphism whose substitution is Method overloading and Method overriding in Java. Thus, today we are going to discuss Method overloading and method overriding in Java. If you remember I shared one real-life story in Inheritance chapter and I am pretty sure that story would have helped you in understanding the Inheritance in a better manner. Similar to that, again I am going to share one of the real stories in this chapter as well so that you could understand the topic like mugging up something. Shall we begin?
Story: Method Overloading and Method Overriding
I have a brother; he is very active in every event at home. He is the one who is responsible to take care of guests, following up with chefs, decorators, and other staff. He is always overloaded with multiple works. Now suppose if he distributes his work among 5 other people, then chances are all the 5 people won’t be reachable on time. Hence, one person who is, however, overloaded with 5 tasks, but, he is reachable and performs the task efficiently is far better than other choices. This is the case with method overloading in Java; the method name would be the same to make the program readable but it will be overloaded with multiple arguments for efficiency.
Now coming to another story, my father built a house, when he built we were only 5 members and 3 rooms were enough to survive. Now I got married so my family also got extended. In this case, I need to add one more floor above to the base. Here what I am doing, my father had something, I borrowed that and made changes as per my requirement, so simply I am overriding the existing resource. This is how the method overriding works. Since I am drawing a father-son relationship, it means I am inheriting something; therefore, method overriding will only work where there are occurrences of Inheritance.
Method Overloading in Java
If multiple methods in java class have the same name, but they differ in parameters is termed as Method overloading. We use method overloading to increase the readability of the program.
How to achieve Method overloading in Java?
There are two ways to achieve method overloading in Java. They are described below.
1. Method overloading by changing the number of arguments
In the below sample program, the addition method performs the add operation for two arguments and three arguments.
package my.project; class Add{ static int addition(int a,int b){ return a+b; } static int addition(int a,int b,int c){ return a+b+c; } public static void main(String[] args){ System.out.println(Add.addition(1,11)); System.out.println(Add.addition(1,11,111)); } }
2. Method overloading by changing data types of the arguments
In the below sample program, the addition method first performs add operation with two integers, and next, it performs add operation with two doubles.
package my.project; class Add{ static int addition(int a,int b){ return a+b; } static double addition(double a,double b){ return a+b; } public static void main(String[] args){ System.out.println(Add.addition(1,11)); System.out.println(Add.addition(1.0,11.11)); } }
Some facts on Method overloading
- If you are passing an integer as an argument, but the method’s return type is double then it will throw a compile-time error
- We can overload the main method, but JVM can only pick that method which has string array as argument for program execution
- We can perform method overloading in case of type promotion, i.e., an int can be promoted to long
- Method overloading fails to work in case of type de-promotion, i.e., double can’t be de-promoted to int
Method Overriding in Java
When there is inheritance and child class declares the same method as declared in parent class with specific implementation then there occurs Method overriding. It helps in achieving polymorphism at runtime.
How to achieve Method overriding in Java?
You need to follow the below rules to achieve the method overriding in Java.
- There must be a parent-child relationship between two classes
- The name of the overriding Method in parent class must be the same in a child class
- The parameters passed in the overriding method of parent class must be the same in the child class
Sample Program
The below example has Dog as parent class and Puppy as child class. In both classes, we have a common method named eating. This method has a specific implementation for each class.
package my.project; class Dog{ void eating(){ System.out.println("Dog is eating"); } public static void main(String args[]){ Puppy obj = new Puppy(); obj.eating(); } } class Puppy extends Dog{ void eating(){ System.out.println("Puppy is eating"); } }
Some facts on Method overriding
- We cannot override the static method, it is declared at the class area, whereas, instance method is declared at the heap area. Therefore, we cannot override the main method as it is static
- The overriding method must not be highly restricted, for example, default is more restricted as compared to protected
This was all about Method overloading and method overriding in Java. I hope you enjoyed reading it. If you have any questions, feel free to connect with me. Do not forget to join our Facebook group.