wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Overloading Concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloading Concept " Watch "Overloading Concept " New topic
Author

Overloading Concept

Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
When we overload any function in a class, either total number of arguments differ or data type of arguments differ, but we don't consider the return type as a differentiating factor. Why?


SCWCD 1.4, SCJP 1.4
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
The compiler likes to look at what happens when you call (invoke) a method; it doesn't much care what the return value is until and unless you assign it. See, with a function, you must use the parameters when you call it, but using the return value is optional. This originated in C (or possibly one of C's predecessors) with the function prototype.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
assuming this would compile:

Which m1 would be invoked?

That's why.

[ November 03, 2004: Message edited by: Ray Stojonic ]
[ November 03, 2004: Message edited by: Ray Stojonic ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
That's not much of an explanation of why so much as an example of why.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
granted.

For the record, I wasn't attempting to correct you, you posted while I was working on mine.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Happens to me a lot!
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
One obvious reason why the method signatures used to choose between overloaded methods do not include the return type is that a method can be invoked in a stand-alone statement with no indication of the expected return type. This would mean that two overloaded mehods are ambiguous when called one way but unambiguous when called in an expression that uses the return value.

float m( float x ) { ... }

boolean m( float x ) { ... }

float y = m(x); // unambiguous

m(x); // ambiguous ??


Mike Gershman
SCJP 1.4, SCWCD in process
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
Thanks for your replies .
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Rohan Kayan
Um....I would like to think about if I am a compiler, which one I want to choose it. Please imagine that both of (methods signature) are the same present, so you can pick up either one which you like.
I would like to accept the one that given by my girl friend.


Francis Siu
SCJP, MCDBA
 
Don't get me started about those stupid light bulbs.
 
subject: Overloading Concept
 
Similar Threads
instance vs object
Overloading doubt
Override - Overload - quick question
Jval Test : Overriding & Overloading
who can explain the method Integer.parseInt( ) to me?