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 Realtime Java Project
Let’s imagine that this class we have created in 2005.
Now, clients are calling this class as given below.
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:
2. Second Option:
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.
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.
Hope that you will have enjoyed this tutorial 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!!!
⏪ PrevNext ⏩