This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method Overloading? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Overloading?" Watch "Method Overloading?" New topic
Author

Method Overloading?

Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
Am I correctly using the idea of method overloading in the below code?


If I understand this right.. both of the calculateFunctions do just somejunk, but one will except a int parameter and the other will except and double parameter and return and integer value.... Do I have basiclly the right idea here?

Thanks,

Pete
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, that's it.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
You have it right and I know it's just a simple example, but the only thing I'd suggest is not naming your methods the same as the class name. Makes me think they should be constructors. Could just be me though!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup. As an example it's fine ... as a design it's scary These two calls give completely unrelated results because the calculations are so different:

calculateFunction(1);
calculateFunction(1.0);



A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
So, Method overloading is made possible by the compiler? Ex. If I say something like....


Method overloading makes the source code more readable, since the compiler know which method to use from the parameters passed then?

Pete
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
i think the definition goes like this:

Method overloading is using the same method name but accepts different parameters.

So I guess yeah you've got the right idea. I do agree though that method names should not be the same as the class name and be more descriptive i.e. getTrash(), getTrash(String garbage).


I'm a Hood Ornament
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
When the parameter types are similar and can therefore be confusing, as in this example, then I would never overload. It is far better to rename the second signature to something like addTen(double x). Bear in mind that just because Java gives you the capability to overload, there is no pressing need to do so.

Of course, there would be no confusion if the parameter types were, say, int and String.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
jyoti Viv
Greenhorn

Joined: Jun 08, 2005
Posts: 3
All above replies define overloading . I want to put some addons in that.

Two or more methods will be overloaded only when their no of arguments or types are different. In case of any other difference in method signature will not make them overloaded.

example :

class Test{
public void test(int){
// code
}

public int test(int){
//code
}
}
above example return type is different but methods are not overloaded there will be compile time error.

class Test{
public void test(int){
// code
}

public void test(int) throws MyException{
//code
}
}

Again above two methods signature is different in exception being thrown. Again methods are not overloaded and will give compile time error.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Method overloading makes the source code more readable


Yes, if we're careful. We really want two methods with the same name to do largely the same thing even if the parameters are slightly different. OutputStream has three write methods that all write to the output stream, and PrintWriter has nine print methods. You'd be surprised if one wrote to the output and another made the speaker beep. Some surprises are good (say a $20 bill in the laundry) but that would be bad.
Sania Marsh
Ranch Hand

Joined: Jul 12, 2004
Posts: 469
I agree with Stan the example you've given in the first post isn't the normal way the overloading would be used. It does different things with two different types just to demonstrate how it works. Normally methods with same name would have very similar functionality, othewise it only gets confusing.
Something like putting coins or dollar bills in the wnding machine - the result is similar, even though the money is of different type.
Or another example - feeding numerical string or number to the dial() method of auto phone dialer, again you would expect similar result in both cases.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Method Overloading?
 
Similar Threads
Same old problems
stars
help with this problem
Math.pow
HashSet