i know this doesn't makes much sense, but i try to relate the prob using an example...i try my best to explain... Lets say i have 2 classes : A Dog class and a Cat class A Dog class which consists of a Leg object (There is a Leg class which have 'number of legs' as an attribute as well as other instance variables..the Leg class also have several methods such as setNumLegs() to set the legs) The Cat class consists of a dog as an object In the Dog class, i can change the num of legs by using the method anyLegs.setNumLegs().. The problem lies here..when i change the Leg object in Dog class how can i make it such that the Leg objects from the Cat class also changes? in another word, whenever some values are changed, they are both updated and same in both Dog and Cat class...
Well one way of solving the problem is to have a "pet" base class from which both Cat and Dog are derived. You could have a variable in the Pet class for the number of legs, which both Cat & Dog would have access to. (If I understand the question correctly.)
Please ignore post, I have no idea what I am talking about.
Bindesh Vijayan
Ranch Hand
Joined: Aug 21, 2001
Posts: 104
posted
0
Kevin, I guess that i understood what you want.Now consider the implementation of your Leg class
This ensures that whenever you change the value of numOfLegs, it gets reflected in both class. Let me know if it was according to your specification. THANKS. [This message has been edited by Bindesh Vijayan (edited September 01, 2001).]
jason adam
Chicken Farmer ()
Ranch Hand
Joined: May 08, 2001
Posts: 1932
posted
0
I think instead what you would want to do is make the integer numberOfLegs in class Legs a static variable, like: That way both Dog and Cat can have Leg objects, but all of those Leg objects are using the same numberOfLegs. However, I think Christopher's suggestion is a better way to go, and more OO in design. However, if the requirement is that one class, in this example Cat, be composed of both Dog and Leg objects, then try making the numberOfLegs static. Jason
[This message has been edited by jason adam (edited September 01, 2001).]