Re: Overriding methods: difference between Java 1.4 and 5+ ?
Andreas Leitgeb wrote:
Philipp <sicsicsic@freesurf.ch> wrote:
public interface IA {
public A getFormat();
public interface IB extends IA {
public B getFormat();
}
}
What can I do to still use this construct on a Java 1.3 compatible JVM?
Change interface IB's method to also return A.
Sorry, no other way. (any class that implements IB will
of course still return B objects at runtime, but they
can't state this fact such that old JVM knows.)
This is called "covariant return", and was introduced in 1.5. An overriding
method may return a subtype ("covariant type") of the parent method's return
type. You cannot have two different overrides that differ only in return
type, though.
In 1.4 and earlier (you do realize that 1.3 has been retired for some time
now, don't you? And that 1.4 hits its "End-of-Life" this very year?)
covariant return types are not permitted.
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8>
Specifically ss. 8.4.8.3.
If a method declaration d1 with return type R1 overrides or hides the declaration
of another method d2 with return type R2, then d1 must be return-type substitutable
for d2, or a compile-time error occurs. Furthermore, if R1 is not a subtype of R2,
an unchecked warning must be issued (unless suppressed (?9.6.1.5)).
--
Lew