Hello,
I wont to have two methods with different result type. Is it really
not possible with JAVA?
public String receivedData()
public byte[] receivedData()
Thank You
Eric Sosman - 01 Apr 2008 14:09 GMT
> Hello,
>
[quoted text clipped - 4 lines]
>
> public byte[] receivedData()
It's perfectly possible, as long as the two methods
belong to different classes. But if they belonged to the
same class, call it Thing, there would be an ambiguity:
Thing thing = new Thing(...);
Object obj = thing.receivedData(); // which one?
I suggest you give the methods different names, perhaps
receivedString() and receivedBytes(). It'll help the user
of the Thing class remember what's what, too.

Signature
Eric Sosman
esosman@ieee-dot-org.invalid
Danno - 01 Apr 2008 19:21 GMT
> > Hello,
>
[quoted text clipped - 15 lines]
> receivedString() and receivedBytes(). It'll help the user
> of the Thing class remember what's what, too.
Hmm, just to add to this, you can overload methods with different
argument types and have them on the same class.
public String receivedData(String s)
public byte[] receivedData(byte[] b)
That won't help out the OP, just thought I'd throw that out. ;)
Lew - 02 Apr 2008 02:45 GMT
> Hmm, just to add to this, you can overload methods with different
> argument types and have them on the same class.
[quoted text clipped - 4 lines]
>
> That won't help out the OP, just thought I'd throw that out. ;)
You can override a base-class method in a derived class and give it a more
specific return type ("covariant" return type) than the inherited method has,
with current Java. As long as the derived method's return type widens to the
base class method's you're good.

Signature
Lew
Roedy Green - 01 Apr 2008 14:37 GMT
On Tue, 1 Apr 2008 05:40:02 -0700 (PDT), column
<column.column@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>I wont to have two methods with different result type. Is it really
>not possible with JAVA?
the language spec is quite clear forbidding it. The one thing you can
do now is override a method that returns Dog with a method that
returns Dalmatian.
See http://mindprod.com/jgloss/covariance.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Chase Preuninger - 01 Apr 2008 15:12 GMT
Doesnt work because the compiler can tell which one you want always.
Say you have two methods, one return an OutputStream and the other
returning a GZipOutputStream. You are then reading it into a variable
of type OutputStream, how can the compiler tell the difference because
either one would work.
http://groups.google.com/group/java-software-develoupment?hl=en
Mark Space - 01 Apr 2008 16:02 GMT
> Hello,
>
> I wont to have two methods with different result type. Is it really
> not possible with JAVA?
Yes and no. No, it's not possible with Java, as far as I know.
The JVM spec is looser however. It just happens I was reading through
the reflection methods recently, and the JVM spec allows objects to have
the same name and signature, and return different results. This (they
said) was mainly intended for tools and things not available to regular
Java programmers.
So if you are willing to work hard enough, you can do it. But you'll
probably have to write your own byte-code compiler, and handle your own
bindings at runtime. That's one heck of a lot of work.
Maybe someone knows an easier way to do it....
Danno - 01 Apr 2008 19:24 GMT
> > Hello,
>
[quoted text clipped - 14 lines]
>
> Maybe someone knows an easier way to do it....
The best way is probably to refactor/redesign what he/she has. There
are many patterns that may help.
rmoldskr+usenet@online.no - 02 Apr 2008 21:55 GMT
> Maybe someone knows an easier way to do it....
Besides rewriting the program in Ada?
They could use a "return variable in the input" idiom and make
do something like the following:
public void receivedData( String[] returnValue ) {
...
returnValue[0] = someString;
}
public void receivedData( byte[][] returnValue ) {
...
returnValue[0] = someByteArray;
}
public doMagic( ) {
String[] stringWrapper = new String[1];
receivedData( stringWrapper );
doStringStuff( stringWrapper[0] );
byte[][] byteWrapper = new byte[][1];
receivedData( byteWrapper );
doByteStuff( byteWrapper[0] );
}
It's rather cumbersome, though. A cleaner way would be to use "marker
parameters":
public String receivedData( String dummy ) {
return someString;
}
public byte[] receivedData( byte[] dummy ) {
return someByteArray;
}
public doMagic( ) {
String receivedString = receivedData( "String" );
byte[] receivedByteArray = receivedData( new byte[0] );
}
It's not a very "javaesque" way to do things, though. It's a pretty weird
thing to do in a java program, and is likely to confuse a whole lot more
than it helps, so I wouldn't recommend it.

Signature
Leif Roar Moldskred