Hi, I have a new doubt about casting. I don' t understand if it's illegal to cast the father class to child class, a class to an interface and an interface to a class (suppose class implements interface). I don't understand the general rule so don't understand why following code compiles ad runs successfully:
Please help me to understand the reasoning behind thi code from line 16 to line 19.
Any Interface can be cast to any non final Class type eventhough the class did not implement the interface also if that interface points to any object that could not be castable then you will get runtime exception.
E.g. Line 16.Class A did not implement the interface Min but still casting is allowed.
SO in this case m is the reference of type interface which is not pointing to any object so that it can be cast to any class type.
Also you know Superclass reference can point to any subclass object but not vice versa and you need a cast to sub type if you are down casting it.
In your case A can point to B without any cast because A is a superclass of B.But if you want to do it other way then you have to cast like
B b=(B)a; //assuming a is of type A
If the reference "a' points to any object that is not B or sub type of B then you will get runtime exception.
1. determine the class relationships: B is an A,
C is a B and is an A
B is a Min
C is a Min
2. look at the reference declarations only
3. check each assignment:
For runtime, you need to check the actual object the references refers to (what's behind the new operator?).
If the object type inherits or implements the assigned type, it succeed without explicit cast;
If the object type is a super type of the assiged type, you need an explict cast, otherwise exception raises.
Siva Masilamani wrote:Any Interface can be cast to any non final Class type eventhough the class did not implement the interface
WHY? This blows my mind off. Ofcourse, you are right. An interface can be cast to another class which does NOT implement it. But that makes no sense. That interface object can NEVER be an object of that class, so why does Java compiler allow it, and why no runtime ClassCastException?
Please take a look at the following code:
This prints the following:
m1 instanceof Diamond: FALSE
This makes no sense whatsoever.
Firstly, why allow this type of casting (line 1) ???
Secondly, how can a variable d1, declared as a type of class Diamond and cast to such, is NOT an instanceof Diamond???
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
That interface object can NEVER be an object of that class
Nope, the cast/instanceof comparison might be successful at run time
You see, the cast/instanceof comparison is allowed by the compiler because there might be a class somewhere that extends that class and that interface. This is why it is allowed. But if the class C is final, then we know that there can't be a sub-class of class C, so the cast/instanceof comparison will NEVER succeed in that case and the compiler will give an error...
At runtime, you can't cast an object to some type that it is not. In this case, a B instance is not a C type.
Joined: Oct 12, 2009
Henry is right. You can always follow this practice to determine whether a ClassCastException will throw or not. To be more specific, what object on the heap does b2 point to? You can do a reverse tracing:
before line 18:
b2 --> a2
a2 --> m
m --> b
b --> m
m = null
You see what b2 currently points to is null. You can assign null to any non-primitive type. The point here is you need to trace back to the most resent new operator or null assignment. If the type behind new operator is not something that can be assigned to Min m2, ClassCastException will throw.