Why the code "pogo = (SwampThing)wawa;" will cause runtime exception but not a complile time error. I think,the "wawa" is Washer type thich is not relevant to the hierarchy from "animal" to "Swampthing".So the explicit casting will cause complile error.But im am wrong. Why???
More descriptive: a subclass of SwampThing might implement Washer so a cast to SwampThing of a Washer might be valid if the Washer being cast is a SwampThing, which cannot be detected at compiletime (though in this simple case the compiler could find it maybe, it's not designed to do so).
posted 12 years ago
JLS Quote: "If S is a class type: ... If T is an interface type: ... If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might). If Swampthing were to be a final class it would resu,t in a compile time error.