Practical Use of Method Overloading in Java Project

In the previous tutorial, we learned the basics of 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 java 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 Realtime Java Project


Use of method overloading in java realtime project


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 given 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. They are as follows:

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 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 this tutorial has covered almost all important points related to the practical use of method overloading in java project. I hope that you will have understood why do we use method overloading in java.
Thanks for reading!!!
Next ⇒ Type conversion and casting in Java⇐ PrevNext ⇒

Leave a Comment