• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question about overloading functions  RSS feed

 
Jon Swanson
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the idea that I could declare two functions like this:



since the arguments were not exactly the same. The JVM tells me that the two functions have the same erasure. I get that they can't have the same signature, my understanding is Java drops what's in the <> and that makes them the same. There is always the workarounds of either creating classes or naming the functions differently. Is there a best practice when it comes to dealing with this issue? I like the idea of overloading functions, but am not that excited about making lots of tiny classes.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the example provided you can treat Double as a special case of Double[] where the size of Double[] is 1.

Coming back to overloading methods- The compiler itself puts in checks to verify the legality of the overloaded methods. Overloading should be done clearly, there shouldn't be ambiguity as to which overloaded method is being invoked. At few places you might want to use a different method name if it is more clear than overloading.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Swanson wrote:I like the idea of overloading functions, but am not that excited about making lots of tiny classes.

The exact opposite of me then. In general, I don't like to see lots of overloaded methods, because you can easily lose track of which one is being called, especially if their signatures are similar. Two is my usual limit.

One solution, although it's generally used as an alternative to lots of overloaded constructors (or factory methods), is the Builder pattern (look it up).

Otherwise, I'd suggest a bit more creativity with your method naming.

Winston
 
Jon Swanson
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I should be defining real classes and passing in a class rather than a hashmap? I have four sets of parameters used for the same calculation and the user decides which one to load. So the original pseudo code was-

pick a set (comboBox)
load the data (loadData)
pick an item from the set (getKeys -> comboBox)
calculate the result for that item (runData)

then someone got the bright idea to use a different calculation method for one of the sets which requires loading more parameters (thus Double[] vs Double). I thought overloading would be appropriate here. If I were loading just Double versus Double[] I think it would work. But Java sees my parameter as HashMap versus HashMap (i.e. the same erasure) if I am understanding things correctly. As it stands now, I have to add conditionals to determine which method to call. The overloading seemed cleaner, had it worked.

The builder looks interesting. I see how it would have worked well in the original implementation where I was always building the same object type. It seems good for ordering a sequence of instructions. It seems like maybe I need to look at Factory patterns because the objects created are different?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!