• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone explain the ClassCast Exception as Thrown in the Code

 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve.. its working now...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic