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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
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.