Objects and Object References

Assigning multiple objects to one object reference

Page 1 of 1

7 Replies - 1479 Views - Last Post: 27 August 2010 - 03:54 AM Rate Topic: -----

#1 Guest_Jason*


Reputation:

Objects and Object References

Posted 26 August 2010 - 09:04 AM

Hello, this is quite a simple query I think, but I realised the other day (when reading about the Decorator design pattern), that I didn't understand the concept fully.
Basically, I saw something like this:

Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);



At this point, I realised I didn't fully understand what this code meant. You are assigning multiple objects to a single reference??? Is this actually what is happening here? I realised I understood how one object reference can be assigned to another object reference, but I didn't totally understand what was happening in the above situation with, seemingly, multiple objects being assigned to one reference. I assumed at the time that one reference can actually point to multiple objects. I am just after clarification really.

Note: The above code was actually Java, but I assume it will be the concept in c#.

Thanks a lot for you time.

Is This A Good Question/Topic? 0

Replies To: Objects and Object References

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Objects and Object References

Posted 26 August 2010 - 09:11 AM

Good link on values and reference types.

The reference points to an instantiated object on the heap. You're just changing to what the reference is pointing with that code. You can do that freely in .NET thanks to the garbage collection feature.
Was This Post Helpful? 0
  • +
  • -

#3 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Objects and Object References

Posted 26 August 2010 - 09:15 AM

No, the reference is not being assigned to multiple objects, it is being overwritten by new objects based on the current definition. Take the following example.

int num = 2; //num is 2
num = 3 + num; //num is 5
num = 3 + num; //num is 8
num = 2 * num; //num is 16


By the end of the code, num is not assigned to multiple numbers. Each operation returns a result and overwrites what is stored in num.

This post has been edited by mojo666: 26 August 2010 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Jason*


Reputation:

Re: Objects and Object References

Posted 26 August 2010 - 09:25 AM

Thanks a lot for the fast reply. So, we obviously have the reference sitting on the stack; everytime a new object is created with the new keyword, and is then assigned to the object reference, the reference then points to that one object that was recently assigned to the reference (which sits on the heap)? That does make complete sense!

However, the reason I assumed that the reference may somehow point to multiple objects in the above scenario is, primarily, that, otherwise, what exactly is the point of the code above? Surely, after the code has finished executing, surely the only object beverage2 points to is the recently created whip object. Therefore, what was the point of creating the other objects and assigning them to the beverage2 reference in the first place? What purpose are they serving? They don't seem to serve any purpose as they only have a reference for a brief moment; up until the point when the next object is assigned to the reference. I am guessing it's something to do with the Decorator design pattern that I am not understanding?
Was This Post Helpful? 0

#5 Guest_Jason*


Reputation:

Re: Objects and Object References

Posted 26 August 2010 - 09:39 AM

That was a very informative tutorial by the way, thanks for that. Thanks for your input as well Mojo666, I definately see why one reference definately doesn't point to multiple objects. It just seems to pointless in many respects, based on what you guys have just clarified for me. What am I missing?
Thanks again.
Was This Post Helpful? 0

#6 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Objects and Object References

Posted 26 August 2010 - 01:07 PM

Take this line of code.
beverage2 = new Mocha(beverage2);  

beverage2 has a value before this line is executed. That value is passed to the constructor of Mocha. I cannot say exactly what the constructor does without seeing the code, but it likely creates a new object based off of the properties in beverage2. beverage2 is then assigned to this new object. You are correct that beverage2 is only set to the last object, but this object could not exist in its final state without the preceeding commands. That's what I tried to show with my example. num is set to 16, but it could not have gotten to 16 without first starting at 2, then adding 3, adding 3 again, and finally doubling the result. Maybe this will better illustrate what the objects are doing.

int num = 2; //num is 2  
num = 3 + num; //num is 5  
num = 3 + num; //num is 8  
num = 2 * num; //num is 16

//assume we have classes objInt, addThreeInt, and doubleInt.
//objInt is practically just an integer that is initialized to whatever is passed to the constructor.
//addThreeInt is an objInt that adds 3 to the objInt that is passed to the constructor.
//doubleInt is an objInt that doubles the objInt that is passed to the constructor.
//the following code is practically the same as the above code.
objInt num = new objInt(2);
num = new addThreeInt(num);
num = new addThreeInt(num);
num = new doubleInt(num);


Was This Post Helpful? 0
  • +
  • -

#7 oldSwede   User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 464
  • Joined: 08-January 16

Re: Objects and Object References

Posted 26 August 2010 - 11:49 PM

Hi!

I'd like to honk my own horn on this subject...

View PostJason, on 26 August 2010 - 04:04 PM, said:

At this point, I realised I didn't fully understand what this code meant. You are assigning multiple objects to a single reference???


In this post I explain how object handling works in a (hopefully) pedagogical way. I think it might clear things a bit for you.

Even though the (simple) example code is VB.NET the concepts shown are the same for most languages, do take a look. :)

Regards
Jens

This post has been edited by jens: 27 August 2010 - 12:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Jason*


Reputation:

Re: Objects and Object References

Posted 27 August 2010 - 03:54 AM

Thank you mojo666 and jens, I think that has finally cleared up the issue for me.

Firstly, Jens, your tutorial was very clear and very helpful. I had a general idea about the concepts you were explaining, but your tutorial solidified those concepts in my head. I particuarly liked the example regarding the signs pointing to the houses. I personally find that, with the more abstract and difficult to grasp concepts in programming, clear, and perhaps crucially, simple 'real world' examples really simplify the concepts and make them much easier to grasp. I very much liked the detail also. I think topics such as object handling need a good amount detail to be demonstrated properly; you have gone into great detail but kept it very clear too.

Oh, and I oftens find that tutorials such as yours lack the clear set of simple coding examples, but your tutorial gave plenty of simple, real code examples to compliment the 'real world examples'. I would give constructive criticism, but to be honest, I can't really think of any! Thank you very much for that, you should definately make more tutorials in the future! Perhaps some on design patterns that haven't been tutorialised on the site yet (since I am reading about them at the moment!).

Mojo666, thank you for your post also. The comment about how

Quote

this object could not exist in its final state without the preceeding commands.
, and the subsequent example using the object creation syntax finally made the concept click in my head.

Thank you to all that have replied in this thread!
Was This Post Helpful? 0

Page 1 of 1