I know There are different way of reprentation for Conversion of Association,Aggregation and composition in java. But when we convert them into code(java classes) they are all represented in same manner. Like Student taught by teacher which is association will be represented with Student class having instance variable of Class Teacher.
Department has professors which is aggregation will be also be represented with Department class having instance variable (array )of Class Professors.
University has departments which is composition will be also be represented with University class having instance variable (array )of Class department.
So all are reprented in same manner in terms of code. So what benefits terms Association,Aggregation and composition provide to developer?
Association in java is one of the types of relations between classes. It has two forms Aggregation(HAS-A) and Composition(Belongs-to). Aggregation is a relatively weak association, whereas Composition is a strong association. Composition can be called a more restricted form of Aggregation.
Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist. Composition implies a relationship where the child cannot exist independent of the parent.
In Aggregation, if we delete the parent object, the child object will still exist in the system. For example, if we remove wheels from a car, the car can function adequately with another wheel. In Composition, if we delete a parent object, the child object also gets deleted.
When an object A contains a reference to another object B or we can say Object A has a HAS-A relationship with Object B, then it is termed as Aggregation. Aggregation helps in reusing the code. Object B can have utility methods and which can be utilized by multiple objects.
You're missing part of the story with Composition. It mandates a couple of extra properties:
You're right that the association will manifest as a reference / collection of references in child & parent respectively. But the code must also enforce the rules above if it's to ensure Composition semantics are enforced.
As for Aggregation: I don't use it. The semantics are too loose and it offers no advantages over straight associations.
hth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With