Method Overloading is a feature in which we can create more than one method having the same name and same class but their parameter lists are dissimilar.
Why method overloading?
Assume that you have to execute multiplication of the given numbers but there can be any number of arguments (let’s say either 2 or 3 arguments for simplicity).
In order to achieve this, you can create two methods num2(int, int) and num3(int, int, int) for two and three parameters correspondingly. Programmers may get puzzled as the behavior of both methods are same but they differ by name. However, The superior way to complete this task is by overloading methods. Also, depending upon the argument passed, one of the overloaded methods is called.
How to perform method overloading in Java?
Here are different ways to perform method overloading:
- A number of parameters.
- The data type of parameters.
- The sequence of Data type of parameters.
Watch this on YouTube – You can also watch the video tutorial or keep reading the blog.
You can also subscribe to the official Pristine Tech School channel and learn various advanced topics with ease. https://www.youtube.com/channel/UCcWR1IezEQynJnAETgK8g2w?sub_confirmation=1
Way 1: Different Number of parameters in the argument list
Way 2: Difference in the data type of parameters
Way 3: Overloading – Sequence of a data type of arguments
I hope this blog will help to explain the concept of Method overloading better. Let me know if we miss on something in the comment section below, we would love to hear from you.