Inheritance - changing one instantiation

Hi

Does it make sense that this displays
3
3
2
2

I can understand why it displays
3
3

but then I though a change to the copy with this line
annie.numberOfLegs = 2;
would display
2
3
for the last two outputs.

Thanks
Paul



Human paul = new Human();
            Human annie = paul;

            paul.numberOfLegs = 3;

            MessageBox.Show(paul.numberOfLegs.ToString());
            MessageBox.Show(annie.numberOfLegs.ToString());

            annie.numberOfLegs = 2;

            MessageBox.Show(paul.numberOfLegs.ToString());
            MessageBox.Show(annie.numberOfLegs.ToString());
paulwhelanAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
no it doesn't, since annie is the same instance as paul. annie only gets an instance reference (somethin like a pointer)
0
 
paulwhelanAuthor Commented:
So in this example when I do

annie.numberOfLegs = 2;

It changes
annie.numberOfLegs = 2;
AND
paul.numberOfLegs = 2;

is that right?

I guess I'm thinking of value types.

int a = 1;
int b = a;

b = 2;

messagebox.show(a); //displays 1
messagebox.show(b); //displays 2

Is all that correct?

thanks
Paul
0
 
2266180Commented:
yes, that is all correct.
you might also think as "everything is an object", so 1 is an object and 2 is a different object
a is an object which get's a reference to object 1. b get's anotehr reference to another object.

in case of annie and paul: annie references paul. so everything that annie points to (members/functions) are actually called from paul. to get teh same behaviour as with the numbers, you will have to create a different instance for annie.
0
 
devsolnsCommented:
has nothing to do with inheritence you simple have so references pointing to the same thing so naturally you'd get those results.
0
 
Arthur_WoodCommented:
however, the real distinction here is between 'VALUE' types, and 'REFERENCE' types.

int is a VALUE type.  This means that with code like this:

int a = 1;
int b = a;

b = 2;


the variable b is in fact a comeplete NEW int, and then line  

int b = a;  

creates NEW location in memory, assigns it the identifier b, and then assigns the VALUE of the variable a to that location.


thus the line  

b=2;  does NOT change the value of the variable a in any way.

on the other hand,

Human paul = new Human();
Human annie = paul;
paul.numberOfLegs = 3;

the Human type is a REFERENCE type, which means that the second line

Human annie = paul;

creates a New location in memory (called annie, which holds a REFERENCE to the variable named paul - annie and paul both REFER to the same VALUE in memory - rememebr that an OBJECT is a reference to another location in memory, which holds the actual VALUE - since an object can be a complex entity, so a single address may not be sufficient to hold all of the objects information.

Thus the line

paul.numberOfLegs = 3;

changes the value stored for the numberOfLegs of the object REFERED to by paul (but that location is ALSO being refered to by annie.numberOfLegs, so annie.numberOfLegs now has the value 2 as well)

AW



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.