aspose file tools*
The moose likes Beginning Java and the fly likes Can someone explain the ClassCast Exception as Thrown in the Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone explain the ClassCast Exception as Thrown in the Code" Watch "Can someone explain the ClassCast Exception as Thrown in the Code" New topic
Author

Can someone explain the ClassCast Exception as Thrown in the Code

AmritP Singh
Greenhorn

Joined: May 10, 2012
Posts: 20
Hi Everyone,

i am preparing for SCJP. This is a Code i got from Kathy Sierra Book. I could not understand as why this code is throwing ClassCast Exception



The Code is Throwing Exception at the following Line
.

Not able to downcast Tree to Redwood.

Thanks Everyone for the Help.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3968
    
  17

When you first call the go2 method you use: go2(new Tree(), new Redwood()); So for that first call, is the Tree represented in t1 a Redwood?


Steve
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Let me give you an analogy in real life. Lets say you have 3 girlfriends. You have a one night stand with all of them. A year later there is a knock on your door and one of your girlfriends comes with a kid in her lap claiming that it's yours. Will you accept that? Obviously no because you have no way of knowing whether she is lying or telling the truth. Similarly, the super class in this case is Tree. It has absolutely no idea about its subclasses(children). So when a class extends Tree, that class knows that it is a child of Tree. The Tree class knows nothing of its children. So the Java Run Time complains saying that you cannot cast a super class type object to a subclass type to be on safer side.


~ Mansukh
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Mansukhdeep Thind wrote:Let me give you an analogy in real life. Lets say you have 3 girlfriends. You have a one night stand with all of them. A year later there is a knock on your door and one of your girlfriends comes with a kid in her lap claiming that it's yours. Will you accept that?

there are some tests. for instance *instanceof* test, if java background .

@AmritP Singh
I see below in your code. I am not fan of this because even it passes instanceof test.

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Haha!! Tests.. Lolz.. Amrit has to decide if he wants to get a "test" done. Frankly speaking, in real life as well as in Java , stay away from instanceof test as much as possible. It is an overhead on "Performance". Ensure that your classes(girlfriends) are loosely coupled and highly cohesive.
AmritP Singh
Greenhorn

Joined: May 10, 2012
Posts: 20
Thanks Everyone ..
@Mansukhdeep :- i remember the explanation you gave .. Thats was a nice one.

My understanding is :-
Superclass does not know about the subclass, so this code gave ClassCast exception. however i would still like to know if there is a way out like using instanceof keyword , that i could make this work.

i tried instance of operator, but now i am getting a different error as "cannot find symbol"

Here is the updated code :-

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3968
    
  17

AmritP Singh wrote:Thanks Everyone ..
@Mansukhdeep :- i remember the explanation you gave .. Thats was a nice one.

My understanding is :-
Superclass does not know about the subclass, so this code gave ClassCast exception. however i would still like to know if there is a way out like using instanceof keyword , that i could make this work.

i tried instance of operator, but now i am getting a different error as "cannot find symbol"

Here is the updated code :-



You shouldn't be checking if the class Tree is an instance of the class Redwood (it isn't) you need to be checking if an object is an instance of the class Redwood. So what object do you think you should put in the if statement instead of Tree?
AmritP Singh
Greenhorn

Joined: May 10, 2012
Posts: 20
Thanks Steve.. its working now...
 
jQuery in Action, 2nd edition
 
subject: Can someone explain the ClassCast Exception as Thrown in the Code
 
Similar Threads
A doubt in the type casting
Down Casting to subtype
Casting
Doubt in Downcasting
Please explain DownCast Code from K&B