Scientech Easy When to Use Method Overloading in Java | Real-time Project Example | Scientech Easy

Saturday, February 9, 2019

When to Use Method Overloading in Java | Real-time Project Example

In the previous tutorial, we learned the basic method overloading in Java with some practical example programs. But there is always the problem to the Java beginners to understand the real use of method overloading in the program or a real-time java project level. They do not understand when to use the method overloading in Java coding? So, In this tutorial, we will discuss the real use of a method overloading in Java with practical scenarios.

When to use method overloading in Java


Use of method overloading with realtime project example.

Let's imagine that this class we have created in 2005.
    class A { public void infoStudent(int age, String loc, int phoneNo){ } }
Now clients are calling this class as below.
Client 1:
    A a=new A(); a.infoStudent(10, "Dhanbad", 9431765890);
Client 2:
    A a=new A(); a1.infoStudent(30, "Ranchi", 9764839020);
 Now suppose in 2010, we get a new requirement of capturing aadhar card of a student. So, what would you do in this case?
To solve this problem, we have two options.
1. First Option:
    class A { // Increasing parameters in the same method. public void infoStudent(int age, String loc, int phoneNo, int aadharCardNo) { } }
2. Second Option:
    class A { // Write another method with some different name. public void aadharCard(int age, String loc, int phoneNo, int aadharCardNo) { } public void infoStudent(int age, String loc, int phoneNo){ } }


Here our requirement is completed in both cases. Now we will consider clients.
In the first option, we will let the client know changes in their existing method. The client will say that
1. We don't require the aadhar card. why should we change our code?
2. Tomorrow if any parameters are added, will you be asking us again to change our code?
3. We will need to do lots of testing for changes. We need to convey our some more client to accommodate our change. Please do not change the methods repeatedly. Write other methods so that if we need, we will call separately.

In the second option, we will let the client know changes in their existing method. They will say
1. Ok, we will accommodate but what changes you have done in your method?
2. Could you explain to us how it will be impacting out code?
3. If everything is the same, the only parameter is increased. So, why you did not give the same name as the method so that we would have not got confused.
4. So lots of issues we will face. 
Tomorrow if new developer joins in a company, he will not have an idea that both methods are the same because both names are very different. 

Now in such a case, we will use the third option. The third option is using method overloading.
    class A { // Write another method with the same name. public void infoStudent(int age, String loc, int phoneNo, int aadharCardNo) { // For the logic for aadhar card, we have just called the existing method. infoStudent(int age, String loc, int phoneNo) } public void infoStudent(int age, String loc, int phoneNo){ } }
How can we communicate with the client?
We have overloaded method infoStudent() for the aadhar card. Now our clients will automatically understand what has happened? They will see the impacts and they will be sure that no logic has been changed overall for an aadhar card only.
So, ultimately the method overloading achieved maintainability and readability of code.

Final words 
Hope that you enjoyed this article and have understood that In the real-time Java project when to use method overloading? Once when you will understand the basic concept of method overloading, you will have not any problem in using method overloading.
Thanks for reading!