File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading Concept

 
Rohan Kayan
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not much of an explanation of why so much as an example of why.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
granted.

For the record, I wasn't attempting to correct you, you posted while I was working on mine.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happens to me a lot!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Rohan Kayan
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies .
 
Francis Siu
Ranch Hand
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic