OO question....please help

falconx88

Senior member
May 18, 2000
351
0
0
I am confuzed about this OO program i am making....

well my question is basically about the design of the object.

if i have an Object A, and this object is inherited by Objects B and C....if B trys to change a public variable within A by one of A's public method's.....then when C trys to access the same variable would C get the newly changed value or is it something completly different.

if the above does not work...how would i design the objects so that B and C both can access methods within A and also gets's the updated changes that A's method modified by objects B or C?

For example: Object A has method called IncrementNUMbyOne(int NUM); (assuming num is one to start with; NUM is a pub var in Object A)
Object B and C inherits object A;
Object B calls IncrementNumbyOne(NUM); (and now num is 2)
if Object C calls IncrementNumbyOne(NUM)---> would num be 2 or 3?? if its 2 how would i design this so that it returns 3.
 

The only way C would be affected by B changing a field in the parent class is if the field was declared static. Otherwise there would be two separate instances of the field when live objects are created, one in the B object, another in the A object.

It is bad practice to declare fields in classes as anything other than "private" You should use public or protected methods to update those fields.

So, to answer your question, have a static int field in A that will contain the data. Have a method in A called incNumber() which will inc the field.