• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Object Casting - Tricky Stuff

 
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

First of all, love the site, some very insightful answers so thank you for that.

A few questions about upcasting an downcasting, I'm getting quite confused with the principles.
I have been following John from Cave of Programming.com, and some implemented some code:

Now I have a main class extending Phone, as follows:

Now we can create some objects of the code:
I think (correct me if Im wrong), the type of the variable (one of the left) decides which kind of methods we can call, but its the type of the object (one on the right) that decides what actual methods get called.

Which creates two objects one of type machine and one of type phone. After this point I get confused.

I have been told this is upcasting, its safe, so Java doesnt need an explicit cast. When we use above, what are we doing? Are we changing the reference variable, or the type of object? Are we creating a new object? Is machine2 pointing at the same object as phone1?

And I guess if I dont understand the first I wont understand this! However I do know we need an explicit cast going down as Java wants to know you know what your doing.

Thanks very much in advance.
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't look like an exam‑related question, so I shall move you to a more general forum.

Standard rule of inheritance. A Phone IS‑A Machine, so you can always assign a Phone object to a Machine reference. You can only now call methods defined in the Machine class however. Which makes me suspicious about adding methods in subclasses.
Don't go on about main classes. You have a class with a main method in. Why do you want to extend phone? A main method ISN'T‑A phone, so don't make such a class extend Phone. Actually, the Casting class doesn't extend Phone after all.

Are we changing the reference variable, or the type of object?

No to both. You have created a new reference but you are neither changing its type nor the type of the object.

Are we creating a new object?

No. It is still the same object. All you are doing by casting is assuring the compiler that this particular machine is a phone.

Is machine2 pointing at the same object as phone1?

Yes.

But whenever I see casts, I start to get suspicious that there is something wrong with the inheritance design.
 
Ains Clark
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply,

Just wanted to clarify a couple of things.

So we are not creating a new object because we are not using the new keyword, but we are creating a new reference variable called machine pointing to the phone object?
What I don't understand is why we use the reference variable and not the type of object. Does it effect phone1? Or does phone1 remain the same?

When we upcast like so, is it the equivalant of saying Phone phone2 = new Phone();? But without the new keyword?

Is there an easy way to remember all of this?! I seem to struggle with the object casting questions on the OCA mocks papers.

Again, thanks for your help.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ains Clark wrote:So we are not creating a new object because we are not using the new keyword, but we are creating a new reference variable called machine pointing to the phone object?
What I don't understand is why we use the reference variable and not the type of object. Does it effect phone1? Or does phone1 remain the same?



First you had a variable named "phone1" which contained a reference to an object of type Phone. Let's call the object "X"; in Java objects don't have names and calling the object "phone1" would lead to confusion with the variable named "phone1".

Now with that line of code you assign another reference to X to a variable named "machine2". Now there are two variables which contain references to X.

Clearly that assignment doesn't affect the variable "phone1" at all. It continues to contain a reference to X. And it doesn't affect the object X at all; objects know nothing about the variables which might have references to them.

So the main thing you need to know is this: An object is not a variable and a variable is not an object. Once you make the distinction then many things besides casting become a lot clearer.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!