javaassociationsaggregation

Is aggregating include define that child object can not belong to another parent object?


I find define aggregation as "We call aggregation those relationships whose objects have an independent lifecycle, but there are ownership, and child object can not belong to another parent object."

And I am interested about "child object can not belong to another parent object", is that true?

Link on definition: https://github.com/learning-zone/java-interview-questions#:~:text=What%20is%20the%20difference%20between%20aggregation%20and%20composition%3F


Solution

  • Not really. Aggregation is defined as a relationship between two objects where an object A "uses" an object B. A and B can exist independently. Furthermore, if A uses B, B can still be used by other objects. The aggregation relationship does not constitute an exclusive binding between the used object and its user. Aggregation is usually presented in all those scenarios where an object provides a service and any other object can ask for it.

    On the other hand, composition is defined as the relationship where an object A owns an Object B. In fact, B cannot exist without being "owned" by A; therefore it cannot live on its own (or it does not make sense to exist on its own). Composition describes all those situations where an object appears as another object's attribute.