Y
York Werres
It would all the same be a nice complement to generics if the 1.5
compiler would indeed issue warnings about user-inserted casts that are
not provably correct. (I.e. those that the compiler cannot simply
ignore). That would give Bracha's comment (that started this confusion)
broad enough scope to no longer be confusing.
I agree very much.
BTW, there's another comment from Bracha in the very same paper that's
confusing (me): He says "The Java virtual machine does not directly support
overriding of methods with different return types. This feature is supported
by the compiler.". What does this mean? Does this mean that the different
return types are gone at run time? Here's an example:
static class A {
static A create() { return new A(); }
public A m(A a) { System.out.println("A->A"); return a; }
}
static class B extends A {
static B create() { return new B(); }
public B m(A a) { System.out.println("A->B"); return (B) a; }
public B m(B b) { System.out.println("B->B"); return b; }
}
public void testOverriding() {
A a_a = A.create(); // static type A, dynamic type A
A a_b = B.create(); // static type A, dynamic type B
B b_b = B.create(); // static type B, dynamic type B
A a2 = a_a.m(a_b); // prints: A->A
A a3 = a_a.m(b_b); // prints: A->A
A a5 = a_b.m(a_b); // prints: A->B <== overriding works
A a6 = a_b.m(b_b); // prints: A->B <== overriding works
B b2 = b_b.m(a_b); // prints: A->B
B b3 = b_b.m(b_b); // prints: B->B
}
public void testReflection() {
try {
System.out.println(
B.class.getMethod("m", new Class[] {A.class}).getReturnType()
== B.class
); // prints: true <== reflection works, too
}
catch (Throwable t) {
throw new RuntimeException("Laufzeitfehler", t);
}
}
So there's obviously some runtime support for overriding methods with a
different return type, because in this example both the overriding and the
reflection worked. Then what did Bracha mean?
2004-05-24, York.