What is that method cast()??? Is it a member of Class class??? I do not
think so!!
Amit :-)
toggles@gmail.com - 23 Nov 2005 17:29 GMT
Actually, yes it is a member of the Class class.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html
or more specificaly:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#cast(java.lang.Object)
Viator - 24 Nov 2005 14:03 GMT
Sorry I am not too much into Java 5.
Amit :-)
> uprrtoa@uprags011> java Test
> Class : class Test$C
[quoted text clipped - 14 lines]
> ^
> 1 error
The compile time type of a is Test.A, and its runtime type is Test.C.
The result of a.getClass() is Test.C.class, but the compile type type is
Class<? extends Test.A>. The compile time result type of Class<? extends
Test.A>.cast is Test.A. There is not suitable dispatch method, so you
get a compiler error.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
toggles@gmail.com - 23 Nov 2005 19:23 GMT
Thanks Tom, thats kind of what I figured but if you add the method
public void dispatch(A a) {
System.out.println("A");
}
Then thats the method that gets called, which seems to me to be
incorrect, as at runtime the result of the cast() call should be
Test.C, thus dispatch(C c); should be called? or am I just missing the
entire point of the cast() method?
Thanks again, I appreciate the help.
Thomas Hawtin - 23 Nov 2005 19:34 GMT
> Thanks Tom, thats kind of what I figured but if you add the method
>
[quoted text clipped - 6 lines]
> Test.C, thus dispatch(C c); should be called? or am I just missing the
> entire point of the cast() method?
I think you're missing the whole point of method overloading.
Overloading is done on the basis of compile-time types.
The result of the cast call is a, statically typed as Test.A. All the
cast method has done was a runtime check that a is of runtime type Test.C.
The entire point of Class.cast is to do runtime checks, not compile-time
check.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/