participate


Java Programming [Archive] - Overloading the return type
<<   Back to Forum  |   Give us Feedback
This topic has 52 replies on 4 pages.    1 | 2 | 3 | 4 | Next »
cvweiss__
Posts:330
Registered: 2/11/05
Overloading the return type   
Mar 2, 2005 7:42 AM

 
I'm well aware of overloading method names, but how about the return types?
Example:

public int doSomething(String str);
public String doSomething(String str);

The compiler doesn't like it when I do this, could someone point out the reasoning for the lack of such overloading?
 
salpeter
Posts:430
Registered: 6/26/00
Re: Overloading the return type   
Mar 2, 2005 7:46 AM (reply 1 of 52)  (In reply to original post )

 
The reason is fairly simple:

public String myMethod();
public Integer myMethod();
public boolean myMethod();


All methods are inherently the same.
What happens if you call:

Object result = myMethod();


Which one would the JVM pick?
And what if the boolean return value was picked, boolean is a native not an Object...

In other words, you can't distinguish which method is called.
 
mythinky
Posts:202
Registered: 2/8/05
Re: Overloading the return type   
Mar 2, 2005 7:48 AM (reply 2 of 52)  (In reply to #1 )

 
Which one would the JVM pick?
And what if the boolean return value was picked,
boolean is a native not an Object...

In other words, you can't distinguish which method is
called.

U mean we can't override the return type???
 
yawmark
Posts:30,122
Registered: 2/3/03
Re: Overloading the return type   
Mar 2, 2005 7:51 AM (reply 3 of 52)  (In reply to #2 )

 
U mean we can't override the return type???

Nope.
 
malcolmmc
Posts:8,610
Registered: 11/9/00
Re: Overloading the return type   
Mar 2, 2005 7:53 AM (reply 4 of 52)  (In reply to #2 )

 

U mean we can't override the return type???

You can't overload it, no. Not even in a subclass.

The JVM picks methods using a signature consisting of name and argument classes. That must result in a unique return type.
 
cvweiss__
Posts:330
Registered: 2/11/05
Re: Overloading the return type   
Mar 2, 2005 7:53 AM (reply 5 of 52)  (In reply to #1 )

 
Well damn, that makes perfect sense. Thanks for clearing it up.

Which one would the JVM pick?
And what if the boolean return value was picked,
boolean is a native not an Object...

In other words, you can't distinguish which method is
called.
 
arturo.de.los.angeles
Posts:1
Registered: 11/18/05
Re: Overloading the return type   
Nov 18, 2005 5:41 AM (reply 6 of 52)  (In reply to #5 )

 
I just want to point out the fact that in PHP it is perfectly allowed to write something like

function myFunction
{
if ($something )
return 0;
else
return "A string";
}

which to me seems like overloading the returning type. It would be great if something like this could be done in Java
 
JoachimSauer
Posts:8,233
Registered: 1/8/04
Re: Overloading the return type   
Nov 18, 2005 5:47 AM (reply 7 of 52)  (In reply to #6 )

 
which to me seems like overloading the returning
type. It would be great if something like this could
be done in Java

it can be:

[/code]
public void myMethod() {
if (...) {
return new Integer(0); //or only 0 with JDK 1.5
} else {
return "a string";
}
[/code]

Now that is definitely an awsome thing to do, because what good common use would you have for a number and a string? They obviously mean different things.
 
aconst_null
Posts:388
Registered: 10/13/05
Re: Overloading the return type   
Nov 18, 2005 5:49 AM (reply 8 of 52)  (In reply to #7 )

 
public void myMethod() {

Im guessing that should be

public Object myMethod {
 
JoachimSauer
Posts:8,233
Registered: 1/8/04
Re: Overloading the return type   
Nov 18, 2005 5:53 AM (reply 9 of 52)  (In reply to #8 )

 
public void myMethod() {

Im guessing that should be

public Object myMethod {


of course, I knew I should have previewed it ...
 
CeciNEstPasUnProgrammeur
Posts:31,612
Registered: 7/23/02
Re: Overloading the return type   
Nov 18, 2005 6:08 AM (reply 10 of 52)  (In reply to #6 )

 
I just want to point out the fact that in PHP it is
perfectly allowed to write something like

which to me seems like overloading the returning
type. It would be great if something like this could
be done in Java

It's not overloading the return type. IIRC, PHP has no "types", which in my eyes is rather a flaw than a benefit.
 
BIJ001
Posts:6,897
Registered: 2003.06.06.
Re: Overloading the return type   
Nov 18, 2005 6:30 AM (reply 11 of 52)  (In reply to #10 )

 
It is a desing decision in the wake of C++.

Some other languages support the overloading based on the return type.

Something like this could be conveivable:
class Pondering {
public Integer tell() { /...
}
public String tell()  / /...
}
void foo() {
Integer I = tell(); // can be resolved
String s = tell();  // can be resolved
Object o = tell(); // out of luck here
}
}
 
CeciNEstPasUnProgrammeur
Posts:31,612
Registered: 7/23/02
Re: Overloading the return type   
Nov 18, 2005 6:36 AM (reply 12 of 52)  (In reply to #11 )

 
What if I choose to disregard the returned value?
 
NelsNathanael
Posts:33
Registered: 8/19/05
Re: Overloading the return type   
Dec 28, 2005 12:29 PM (reply 13 of 52)  (In reply to original post )

 
So, why not just allow method overloading based on return types when the return type is a primitive?

int i = getProperty(key);
boolean flag = getProperty(key);

instead of
int i = getProperty_int(key);
boolean flag = getProperty_boolean(key);


That'd be nice. Even nicer would be to allow return type method overloading across the board, but throw an error or a warning at compile time if method usage was ambiguous.

String str = getProperty(key);
Integer val = getProperty(key);
Object o = getProperty(key); // ERROR!  WARNING! Ambiguous method invocation!  (Me stupid compiler and me don't know which one you mean!)


Unless, GASPS, there were a
public Object getProperty(String key) {return null;}
defined somewhere. I'm sure this entire issue has already been debated to death elsewhere. Pointers? Links?

Nels
 
DrClap
Posts:38,727
Registered: 4/30/99
Re: Overloading the return type   
Dec 28, 2005 1:27 PM (reply 14 of 52)  (In reply to #13 )

 
So, why not just allow method overloading based on
return types when the return type is a primitive?

int i = getProperty(key);
boolean flag = getProperty(key);

Consider this:
public class Bling {
  public static void fumble(int value) {
    // some implementation
  }
  public static void fumble(boolean value) {
    // some implementation
  }
}
followed by
Bling.fumble(getProperty(key));
Now which of the possible pairs of overloaded methods should the compiler choose?
 
This topic has 52 replies on 4 pages.    1 | 2 | 3 | 4 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 63
  • Guests : 118

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums