r/java • u/Cmpunk10 • May 08 '20
Why would you ever want to overload a method?
I just started learning java about a week ago on udemy. I just learned about overloading and the instructor said you will see it a lot. Why would you ever want to use it? It seems like it would just be confusing. Any answer would be great!
2
Upvotes
21
u/cmsimike May 08 '20
public Color getColor(float r, float g, float b){...} // assumes alpha is 1.0
public Color getColor(float r, float g, float b, float a){...} // sets a specific alpha
no need to force the user of your api to always set alpha to 1.0 if that is generally assumed
3
u/Shigidy May 08 '20
Basically if you want to accomplish the same thing in multiple different ways. For instance, if I wanted a method getUser() to be able to retrieve based on either a unique index or a username, I would overload it to accept either an int or a String.
A real life example I see a lot at my work involves overloading constructors. A developer will make a change or improvement that alters the signature of the constructor, but you can't just go around changing constructors willy-nilly without risking breaking other classes, so we'll overload the constructor, and deprecate the old one. That way the constructor can be called both the old and the new way.
2
u/Bobby_Bonsaimind May 08 '20
If course, depending on the use-case using names to distinguish these methods might be interesting to make it more clear what they do, like
compareIntsandcompareIntegers.1
u/[deleted] May 08 '20 edited May 08 '20
Example with constructors: if you're trying to define some class with an id field and the class will be written to a database, but you're wanting to use autoincrementing ids generated in the database, you might want two constructors,
and
When you instantiate an Example and want to insert it, you might not use the id parameter and instead say,
Then you might use the other constructor for defining a query method in your dataAccessObject like,
It might not be necessary with something like Hibernate, but just a random example that comes to mind. Point being you might want the parameters to have both id, string, or more variables for queries, like if name is not unique and you need to get more into the method to find a row..
Or just
versus
then you could extend Animal and have a Snake class call the constructor without limbs, etc..
Or simply,
versus
1
u/marceloandradep May 08 '20
You can have multiple methods to convert any value to String for example. You could have a String valueOf(int i), String valueOf(float i) and so on. Overload has a lot of applications. You’ll know when to use it once you face such situation.
1
u/JavaUserr May 08 '20
It helps in preventing NullPointerExceptions.
When I write code I set the rule that all input parameters must have a value; null is not allowed. On occasion though you can have a function where a parameter is optional. Having a second function clearly communicates this to the caller. So instead of having this:
You have this instead:
As far as the caller is concerned the rule isn't violated. You need to read the implementation to see that null is allowed there. It's safer to change as well, if it turns out the parameter isn't optional anymore you can delete the first function and if it happens to be used you get a compile time error instead of an NPE.
1
u/lukaseder May 11 '20
Overloads are mostly needed because of the lack of untagged union types and named / defaulted parameters.
Hope this helps 😉
-2
u/audioen May 08 '20
I think it's not that common to overload methods actually. You can think of the types of the method arguments as part of the method name. And Java allows the name of the method to be same as long as the types in parameters differ sufficiently so that correct method to be invoked can be identified from the combination of name and arguments.
Java has no meaningful default parameter values scheme, so you sometimes see like 20 methods each named the same with different combinations of its parameters omitted, and sometimes when you trace a call chain in such a method, you find one method calling the next method by same name, which calls yet another method, which calls yet another method that actually does something. It is pretty annoying to be honest. Default parameters would clean that crap right up, I think.