jQuery in Action, 2nd edition
The moose likes Beginning Java and the fly likes casting and converting objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "casting and converting objects" Watch "casting and converting objects" New topic
Author

casting and converting objects

Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
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 :



an island in the sun <br />with a language of many tongue?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24039
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
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

Joined: May 23, 2005
Posts: 55
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
Marshal

Joined: Jul 08, 2003
Posts: 24039
    
  13

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!
 
I agree. Here's the link: http://jrebel.com/download
 
subject: casting and converting objects
 
Similar Threads
static Methods @ Interface
Conversion
cast
my notes on JLS for any1 who needs them !!
java.lang.ClassCastException: Parent