> When I cast subClass1 to RandomInterface I get a ClassCastException at
> runtime. However if I try and cast subClass1 to SubClass2 then I get a
> compile time error. Does java apply different casting rules to class
> and interfaces?
If the class isn't declared final a subclass can implement an
interface the super-class hasn't. So during compile-time a
non-final class might be implementing an interface the compiler
isn't aware of so the compiler delegates the decision to the
runtime. Casting a class to another that is not a superclass,
the compiler already knows during compile-time that it doesn't
work and can deny that.
If you declare SubClass1 final, you will get a error during
compile-time as well.
Regards, Lothar

Signature
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
iamrichardjones@googlemail.com - 07 Oct 2007 16:01 GMT
> iamrichardjo...@googlemail.com wrote:
> > When I cast subClass1 to RandomInterface I get a ClassCastException at
[quoted text clipped - 20 lines]
> Always remember: The answer is forty-two, there can only be wrong
> questions!
Hi Lothar
Thanks for that explanation. That makes sense now
Richard