implementing method with different return type

M

Mike Schilling

Joona I Palaste said:
Mike Schilling <[email protected]> scribbled the following:

Yes, that's what I meant to say. It was almost midnight and I had had a
glass of Foster's when I wrote that so I didn't get my terminology quite
straight.

On the one hand: precise terminology in a usenet post.
On the other: a glass of Fosters.

You made the right choice.
 
T

Tony Morris

Given that one overrides the other, the compiler doesn't need to decide.
JDK
You can't have two methods in the same class that differ only in
return type.

I don't believe that this was ever said. i.e. you are stating the same thing
(unless I read it incorrectly).
Either way, I wouldn't be instructing the language compiler author about how
Java
works now and/or in the future.
:)

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
B

Bent C Dalager

For example, if you have a pair of instance methods

void f(String)
void f(Object)

there would be be no way to override the second without also overriding the
first.

I'm not sure why this would be much of a problem. You could
conceivably still invoke the first by calling it with a String object?

You could also override both of them and still be able to call either
by casting to the type of the one you'd want.

Not that I think any of these represent particularly good practice,
but nevertheless :)

Cheers
Bent D
 
N

Neal Gafter

Roedy said:
You can't have two methods in the same class that differ only in
return type.

...

1.5 restricts you to return a subclass of the base class return
method.

1.4 insists they exactly match.

Actually, 1.5 restricts you to a subtype, not a subclass.
 
N

Neal Gafter

Bent said:
I'm not sure why this would be much of a problem. You could
conceivably still invoke the first by calling it with a String object?

Not if it is overridden.
 
C

Carl Howells

Roedy said:
What does that mean?

Is there some primitive mismatching allowed too?

No, primitive types must be exact.

It means that you can override a method that returns java.util.List<E>
to return java.util.ArrayList<E>.

ArrayList<E> is NOT a subclass of List<E>, because List<E> isn't a class.

Even more interestingly, I believe there's also some degree of ability
to change the parameterization... I think you could override a method
that returns List<Number> with a method that returns List<Integer>. Not
as sure on that one, though.
 
N

Neal Gafter

Roedy said:
What does that mean?

Is there some primitive mismatching allowed too?

No, though we did briefly consider that.

A class is a subtype of any interfaces it implements, for example. There is no
"subclass" relationship for interfaces. There are also new subtype
relationships introduced by generics and wildcards; for example the type

List<String>
is a subtype of
List<?>

this will all be spelled out in excruciating detail in the third edition JLS.

-Neal
 
M

Michael Scovetta

Pradeep,
I think from a philosophical point of view, one danger would
be the inability to distinguish compile-time invalid casts:

class three implements one {
Object methodOne() { return new Vector(); }
}

two a = new two();
three b = new three();
XXX(a);
XXX(b);

void XXX(one z) {
String x = (String)z.methodOne(); // valid cast or not??
}

---------------------
of course, this is only compile time, at runtime the vm would
know that either z was a two or z was a three, so it would either
raise an exception or not. I can't think of any reason why allowing
this would be dangerous at runtime, so maybe that's why 1.5 is
relaxing the restriction.

Michael Scovetta
 
D

Dale King

Hello, Peter!
You said:
Does this also mean that methods could be "overloaded" like this:

void foo();
int foo();
Long foo();

No, you can only override with a subclass of the return type of
the inherited method signature. Previously, it had to be the
exact same class and in 1.5 they relax that to include
subclasses. It's caled covariant return types and is a necessity
with the introduction of generics.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top