if:
interface I{}
class A implements I{}
class B extends A{}
class C extends B{}
A a = new A();
B b = new B();
Why a = (B)(I)b; is correct
but b = (B)(I)a; is false?
I find casting to be very confusing, what is the best way to understand if I can down cast or up cast an object?
解决方案
The only thing of relevance to answering the question in your coding sample is the following:
class B extends A{}
This means that B is a subclass of A. Subclasses can be cast to super class types, but super class cannot be cast to subclass types.
Therefore, A cannot be cast to type B.
Why? Think about the logic this way:
is a type of Programming_language, but Programming_language is not a type of