Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Can someone explain the ClassCast Exception as Thrown in the Code

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve.. its working now...
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic