• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

question on overloaded methods

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i have a question on overloaded methods is the following method valid? why if not?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will a class with such methods compile? If not, what does the compiler complain about?
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is what i want to know. this is a question from my assignment. will such a overload method works.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is this the way to overload??
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where's my pin cushion? Hi Marilyn! Nice day here in Switzerland. How's the weather there?
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it means that i can do the overloading like this??
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it means that i can do the overloading like this??
Does it compile?
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually it does compile, i did not get any errors
.you are saying thats not the way??
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks for the promptly replies u guys are great. thanks again for clearly up my confusions.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and mine too.....thanks...
 
reply
    Bookmark Topic Watch Topic
  • New Topic