tags:

views:

112

answers:

3

Is it possible to have one parent object for more than one child object so that all the child could share the same parent state?

A: 

Yes that is possible. I have an ArrayList class for that, an ArrayList is actually a normal array but I only allow objects to be in it.

Take a look at the ArrayList class from Microsoft, mine is based on that one.

Ben Fransen
I'm on PHP btw ;)
Ben Fransen
A: 

The Flyweight-Pattern might help you. Or not. For a more specific answer, please make your question more precise, best would be a description of you actual use case.

Space_C0wb0y
+1  A: 

If your Child class derives from Parent then a Child isA Parent. If you create two Children then they are separate objects and their Parent "parts" are separate. That isn't the effect you are asking for.

I assume that scenario you want is that there can be several families. There's a Parent (call him Fred, age 72) and a Parent ( call her June, age 45)

Fred has children F1, F2, F3, June has children J1, J2.

All of Fred's children have age 72, June's 45, and when we pass Fred's birthday all his children automatically age to 73.

So we model this by a hasA relationship.

Child { 
     Parent myParent;  // points to Fred or June, or whoever
     int getAge() { return myParent.getAge(); }
}

Note that we end up delegating to our parent, which is slightly more work than using inheritance, but is probably what you want to do.

djna