• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Method Overloading?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's it.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, You are going right.
Method Overloading means defining different methods with same name in same class. The name of method can be same but the parameter list (no and type of parameter) should be different.

It is also called as Static/Compile time polymorphism.

Example: You can have one class, where you can define different methods with same name area() having different implementation like to find area of square will be different than area of rectangle.

As per the parameter list, method will be called.

For more Details on Method Overloading Refer following link:
Method overloading
 
Marshal
Posts: 68956
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

manisha Mulchandani wrote: . . .
Method Overloading means defining different methods with same name in same class. . . . It is also called as Static/Compile time polymorphism.

Example: You can have one class, where you can define different methods with same name area() having different implementation . . .

I would disagree. I would suggest that compile time polymorphism is at best a misleading term, and should be avoided.

I would suggest that overloading methods to find the areas of different shapes breaches the principles of object‑oriented programming. The methods should be in the shape classes and should not be overloaded.

Did you notice you have posted on a ten‑year old thread?
 
We don't have time for this. We've gotta save the moon! Or check this out:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic