Originally posted by Michal Adamski: Why compiler allows cast classes for any interface?
The compiler will allow a cast to an interface, when it can't be sure if the object implements that interface.
Try casting a String object to the Map interface. It will not work -- the compiler will complain. The reason is because the String class is final, it doesn't implement the Map interface, nor can it be subclassed to create a String object that implements that interface.
gives an idea of what you can do. Notice that the cast to A compiles but thing is not actually referring to an A, so it fails at runtime. In the case of the cast to B, thing1 actually refers to a B so it does not give an exception.