wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCastException" Watch "ClassCastException" New topic
Author

ClassCastException

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Why does this code give a ClassCastException at runtime

Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Angela,

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.

Cheers,

Gian Franco


"Eppur si muove!"
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
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.
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
I am sorry still can't get the concept
Why doesn't this code give ClassCastException



Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,

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.

Cheers,

Gian Franco
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Angela,

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:



I hope that helps,
Corey


SCJP Tipline, etc.
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Thanks for replying

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
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Lets try to visualize the answer.


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 ]
Beny Na
Ranch Hand

Joined: May 26, 2004
Posts: 159
HI,

a dog is an animal,
but animal is not always a dog, it can be anything..therefore you got a class cast exception.

in your example
: B extends A

from corey example : Dog extends Animal Cat extends Animal
SO,

dog is an animal but Animal is not always a Dog.
B is an A but A is not always B(therefore you got a class cast excep).

correct me if i am wrong corey,

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException