This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

casting and converting objects  RSS feed

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently learning about casting and converting objects and have come across what seems to be a contradiction from program to discription of what actually happens when implicit conversion takes place. I am told in referal to my manual that child is implicitly cast to a class of type Object and yet when I print the values of obj of type object it holds the opposite value .I understand the concept of the heirachy of implicit casting but why is it explained as being cast into an Object object and yet it holds the value of the class being passed to it here`s the code :

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

If an object is an Animal, then a variable is like a Leash. The Leash is not the Animal; it's just the mechanism by which you have access to the animal. You could attach several Leashes to the same Animal, just as you can have several variables refer to the same object.

A cast (implicit or implicit) affects only the variable -- the Leash. It doesn't effect the object (the Animal). If I have my Pet on a Leash, and you're a professional Pet walker, and I hand you the Leash, I'm implicitly casting that Leash to refer to a generic Pet. It's not a generic Pet leash of course -- it's attached to a specific kind (subclass) of Pet. So let's say it's an Alligator Leash. You're a Pet walker, so I can give you any kind of Leash that's attached to a Pet. This has no effect whatsoever on my pet Alligator on the other end of the Leash -- he's still an Alligator.

Now, if you're walking along, and somebody tells you "Aaaaaaaah! There's an Alligator on the end of that Leash!" then they're doing an explicit cast from the superclass (Pet) to the subclass (Alligator.) Now you know there's an Alligator, and you'd be wise to treat it as such. But only your knowledge has changed -- the Alligator has always been an Alligator.

Make sense? You could also have a look here and here for another way to explain the same thing.
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I`d just like to say the two web pages that you have posted to explain the problem in hand are the best explanations I`ve come across so far. This problem of pass by value of primitives and objects has been confusing me for weeks ,but drinking a cup of coffe will never be the same again
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for the help so far.I (think),I now understand the concept of handling a reference to an object

ie Cup c = new Cup();// declares and initialises the object
Cup d = c;// another variable points at the same object

Cup e = new Cup();/*another cup object has been declared and inialised
and refering to a seperate object than c& d*/

As I said I think that is what is happening, but what is happening here-

Object obj;/*declare a variable that wiill eventually be assigned to a
Object object*/

obj = new Cup();/* I`ve changed my mind I`m going to assign it to a Cup
object instead*/

Why are we declaring an Object(or any superclass) with a variable and then referencing it to a different object.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

All your assertions are correct. Now, as to your last question: if you enjoyed the last two campfire stories, then go on and read the next one "How my Dog Learned Polymorphism" and it will answer it for you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!