Link to home
Start Free TrialLog in
Avatar of paulwhelan
paulwhelan

asked on

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());
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paulwhelan
paulwhelan

ASKER

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
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.
has nothing to do with inheritence you simple have so references pointing to the same thing so naturally you'd get those results.
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