aspose file tools*
The moose likes Beginning Java and the fly likes question on overloaded methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question on overloaded methods" Watch "question on overloaded methods" New topic
Author

question on overloaded methods

Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
hi i have a question on overloaded methods is the following method valid? why if not?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Will a class with such methods compile? If not, what does the compiler complain about?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
that is what i want to know. this is a question from my assignment. will such a overload method works.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Why not write a class with these methods, compile it, and see what happens? Using the JDK yourself is one of the best learning tools avilable to you. We can certainly help with questions like "why doesn't this work" or "how does this work", but we don't want to do everything for you.


"I'm not back." - Bill Harding, Twister
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22

is this the way to overload??
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
i think i am confuse, the last post was on overloading constructors, what i did to overload methods was changing the int dest and String around. how come this works. can someone explain to me

i m not very sure if this is the way to overload a method but from some site i read the show it this way.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
An overloaded method is a method with the same name but with a different signature. By changing the order of the parameters, you have changed the signature.

What happened when you had the same signature (the same parameters in the same order) with a different return type (int or void)?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
the editor show this error message btw i m using bluej to learn java so the error message is from bluej
the error msg
MyWeight(int,java.lang.String) is already defined in overload public void MyWeight(int dest,String s)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
That's exactly the point. The compiler cannot distinguish between the two methods if they have the same signature ( the list of parameter types ) but have different return types.

This will not compile. The types of the parameters correspond one for one. int - int, String - String. But the return types differ: int - void
If you have the following code:

Which method would the compiler call? the one returning an int, or the one returning nothing (void)? Result: compiler meltdown.
Overloading occurs only when you have a difference between the lists of types in the parameter lists. A difference in number of parameters, or a difference in the correspondence of the parameters. The return type (and the method name, of course) must remain the same.
By the way, when BlueJ displayed the error message, did you press the "?" button? Sometimes you get more details as what the problem is caused by.
[ March 16, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
So the moral of the story is that if the parameters in the parentheses are identical (and in the same order) and the name of the method is identical, the compiler thinks it is the same method (not an overloaded method) even if the return type is different.

In order to be overloaded, the parameters must be different while the method name remains the same.
(jinx, Barry)
[ March 16, 2003: Message edited by: Marilyn de Queiroz ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Where's my pin cushion? Hi Marilyn! Nice day here in Switzerland. How's the weather there?
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
So it means that i can do the overloading like this??
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
i have another question i know about private but i saw this protected private what does it actually do? is it the same as private??
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
So it means that i can do the overloading like this??
Does it compile?
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
actually it does compile, i did not get any errors
.you are saying thats not the way??
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
..this protected private what does it actually do? is it the same as private??
You can likely ignore that old combined modifier. It's no longer supported in Java.
Today, we've four levels of access modification to be concerned with: private, default (no specifier), protected, and public.
If it's private, it may be accessed from within the same class only. If it's default (also known as package), then it may be accessed by any class in the same package. If it's protected, then it may be accessed by any class in the same package or any class that is a subclass. If it's public, any class may access it.
Take a look at Sun's Java Tutorial on Controlling Access to Members of a Class.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
it does compile, i did not get any errors
.you are saying thats not the way??

If it compiles, it's allowed.
If you're implementing this in a real application, I would suggest that you might want to consider a different interface. Having two methods with the same name and the same arguments (though in different order), but with different return types is a bit odd and would likely cause other programmers using this class to stop and think too hard.
[ March 17, 2003: Message edited by: Dirk Schreckmann ]
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
ok thanks for the promptly replies u guys are great. thanks again for clearly up my confusions.
Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
and mine too.....thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on overloaded methods