This question already has an answer here:
Since I have found so many examples and conclusions about Method Overloading but still I am confusing in real time how we are using it.
First of all method overloading is a class level activity means within that class we are overloading a method with same name but different arguments like
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}
and after we are calling this method like
public static void main(String args[]){
ClassName obj=new ClassName ();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
Suppose instead of this I will take two separate method like
void method1(int a,int b){System.out.println(a+b);}
void method2(double a,double b){System.out.println(a+b);}
And I will call this 2 methods same as above
public static void main(String args[]){
ClassName obj=new ClassName ();
obj.method1(20,20);
obj.method2(10.5,10.5);
}
In both the case for both the methods the activities are same then what is the exact need of overloading/polymorphism in this case.
Somewhere I found that Method overloading increases the readability of the program can anybody please specify that only because of this line we are using method overloading.
Thanks.
Suppose we didn't have method overloading in Java.
Instead of :
we would have :
or even worse :
Both alternatives would make the code less readable.
Here is an example of why method overloading is necessary in order for developers like us to be able to use them effieciently:
In the Graphics2D class in java there is a method called drawImage, although it has an overload as shown below: abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) abstract void drawImage(Image img, AffineTransform xform, ImageObserver obs)
Suppose that instead of being organized as shown below, they were labeled drawImage1 and drawImage2, that would make it so that any version of drawImage has a completely different name for you to remember thus meaning you could potentialy end up having to remember many names of every drawImage method instead of simply knowing what parameters are acceptable. Therefore, method overloads provide developers with the different methods that have different functionality, but the same name because they essentially do the same thing, but require different parameters to do it just like the drawImage overload.
Yon can find more details about overloading methods in oracle documentation page.
In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method.
Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list
.The note provided in documentation page exactly opposite to your statement (overloading increases code readability)
Have a look at one example.
Square
passes it's length as parameter.Rectangle
passes it's length and width as parameter.One more 3D shape
passes it's length, width and height.The
sifhtPosition
is a overloaded method with three different signatures.Assume a scenario, where you have 8 different versions of the same method. If you try to find the references of each individual method across the project classes for code refactoring, you will realize that it's a big mess.
This javaworld articles explains about costs and dis-advantages of method overloading.
You can check the existed Java API like
Math
,Arrays
to see the power of Overload.Do you want to remember each method name depends on your data type or just use Arrays.sort() and let language itself decides which implementation should be used depends on the parameter?
For some other scenario, you may have dozens of methods that have same function but the implementation just vary a little based on dozens of combination of parameters, the situation will become worse if you do not use Overload.
Same discussion has already been discussed in this thread.
Why should I ever overload methods?
Method overloading can also help API designers to provide different levels of abstraction.
For example, she can choose to provide a beginner version, where most parameters are given default values that works well in most case, in parallel with an expert version where the user can fine-tune the parameter to suit their particular needs:
A real world example from
HashMap.java
:Usually the user do not need to play with the
initialCapacity
andloadFactor
parameters, but they can if needed.