Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Sully Sylvester
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
when you have class X and Y and Y extends X.

So assuming i have the following correct.

X x = new X(); (1)
Y y = new Y(); (2)

x = y; (3)
y = (Y)x; (4)

But if you remove the line at (3) line (4) will fail at runtime. My question is why is line (3) neccessary for line (4) to work at runtime and what difference does it make?

Cheers.
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y is a child of x.
You can't cast parent to child, because parent knows nothing about his children.
 
Sumit Deshpande
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

X x = new X(); (1)
Y y = new Y(); (2)

x = y; (3)
y = (Y)x; (4)


At line 3, you are telling x to refer an object of Y at runtime. So, you can "get the object back" from x at line 4. But if you remove line 3, then x is pointing to an Object of X (which has no knowledge about Y). So obviously you cant ask x something which it doesnt know about.
Hope this helps.

-Sumit.
 
Sully Sylvester
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that clears it up .
Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think? Should Johnny Depp have been casted as Willy Wonka?

Oh, sorry I though this question was about movie casting.

Sorry, go back to your studying and ignore me.

Mark
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

���� Huh

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic