During compile time the compiler looks if everything related to casting is ok by taking the reference variables. The code is fine, because B IS-A A and in main an explicit cast is done in order to tell this to the compiler:
During runtime though you get the ClassCastException, because Type A is the super class of type B, so an instance of type A cannot be converted to the type of the subclass B.
"Eppur si muove!"
Joined: Mar 01, 2004
Thanks for replying.
Will such a cast always result in ClassCastException or there can be cases when despite of such casting ClassCastException will not be there at runtime.
Joined: Mar 01, 2004
I am sorry still can't get the concept Why doesn't this code give ClassCastException
Joined: Dec 16, 2003
This code is actually a reply to your question whether a ClassCastException will not be there some of the time.
In this case the reference type of o1 is A and an instance of C is assigned to it. Since C's super type is B (i.e. C IS-A B) at runtime the cast won't give a problem.
It's important to make the distinction between compile time and runtime. In the former you are looking at references in the latter you look at the actual objects that are instantiated.
When you say that one class extends another class, you can say that the subclass "is a" type of the superclass. Look at this example:
In this case, the class Cat extends the class Animal. Therefore, we can say that a Cat "is a" type of Animal. Likewise, Dog extends Animal so we can say that a Dog "is a" type of Animal. We say this because, through inheritance, Cat and Dog take on all of the aspects of an Animal. In addition, however, both Cat and Dog define some extra details about themselves that the generic Animal class does not have. Therefore, an Animal "is not a" Cat and an Animal "is not a" Dog. Similarly, Cat and Dog have no such relationship. They are "sibling" classes. A Cat "is not a" Dog and a Dog "is not a" Cat.
You get a ClassCastException when you attempt to cast an object as something that it "is not." For example, you can safely cast a Cat as an Animal because a Cat "is a" type of Animal. However, if you try to cast a Dog as a Cat, you get a ClassCastException because a Dog "is not a" Cat. Here's an example:
You get a ClassCastException when you attempt to cast an object as something that it "is not." For example, you can safely cast a Cat as an Animal because a Cat "is a" type of Animal. However, if you try to cast a Dog as a Cat, you get a ClassCastException because a Dog "is not a" Cat.
But as in the code above
class B extends A so "B is a A" .Then why is there a ClassCastException
Suppose it does not cause the class cast exception at runtime what will happen?
B instances can have additional members(fields and methods) than A instances.
b is a reference of type B.
You as a programmer if given b as a handle will try to access the extra members found only in B.
But since b is actually pointing to an A instance which does not have those members you will get a meltdown when u try to use members that dont exist. For this one reason alone there should be the class cast exception to avoid potential "method/fields not found in object" exceptions.
If you follow the same line of thinking for the following code it can never cause the same problems because A will always have less members than B and all members of A will be in B for sure.
[ June 10, 2004: Message edited by: Swamy Nathan ]