6 Replies - 235 Views - Last Post: 01 February 2013 - 05:22 PM Rate Topic: -----

#1 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Question: Casting Objects

Posted 01 February 2013 - 04:36 PM

I had a question regarding casting objects. I have always used the following syntax to create an instance of a class
Person me = new Person();


My java textbook is explaining implicit/explicit object casting and I need further clarification to feel warm and cozy.
Object me = new Person();


By implicitly casting an object in this way, what is the difference between my code and the casting? I do understand that a Person() is always an instance of Object, but Object is not always an instance of Person(), but I guess I don't just quite grasp the reasoning behind it.

Is This A Good Question/Topic? 0
  • +

Replies To: Question: Casting Objects

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Question: Casting Objects

Posted 01 February 2013 - 04:48 PM

The variable me is of type Object, which means that it can reference any value of type Object, which includes child classes. Since every class, including Person, extends Object, your variable can point to a Person object.

You would need an explicit cast if you were to use Person-specific methods on your variable. Since me is of type Object, you can only invoke Object-specific methods on it. So if you cast: ((Person)me).setName("newName");, then you can access a Person specific method.

So basically, when you are going to a more specific class, you have to cast. When you are going up the hierarchy, you don't have to explicitly cast.
Was This Post Helpful? 2
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Question: Casting Objects

Posted 01 February 2013 - 04:49 PM

In JAVA, Every class extends the Object class


genneral var=(general) specific
general g=specific

so it is

GGS-GS

Object o=(Object) new me();
Object o2=new me();

This post has been edited by burakaltr: 01 February 2013 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: Question: Casting Objects

Posted 01 February 2013 - 05:00 PM

I think I understand what you are getting at. I just need a tad bit more clarification and I'll feel good about it.

Quote

You would need an explicit cast if you were to use Person-specific methods on your variable. Since me is of type Object, you can only invoke Object-specific methods on it.

Let's say I created an instance like so.
Person me = new Person();


And in the Person class contained a method to set the name variable. I can set this just using..
me.setName("Bob");


So in what scenario do I cast the object first? Sorry if I seem a little dense here, I'm just trying to wrap my head around the situation where I would apply this.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Question: Casting Objects

Posted 01 February 2013 - 05:12 PM

You don't have to cast there because the variable is of type Person. If I had:
Object me = new Person();
me.setName("newName"); //error because setName() isn't defined for type Object

//success- you are telling Java that the reference is of type Person
((Person)me).setName("newName"); 


Was This Post Helpful? 1
  • +
  • -

#6 FerretHolmes  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 167
  • Joined: 12-November 12

Re: Question: Casting Objects

Posted 01 February 2013 - 05:20 PM

Gotcha. I have no idea why that was so difficult to understand, I think I was making it more difficult than it needed to be.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,668
  • Joined: 27-December 08

Re: Question: Casting Objects

Posted 01 February 2013 - 05:22 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1