Aggregation and Composition in Java Code

前端 未结 2 1408
猫巷女王i
猫巷女王i 2021-01-06 14:56

As the Aggregation and Composition is related Association or we can say it gives the understanding of relationship between object or anything else.

I posted this que

2条回答
  •  爱一瞬间的悲伤
    2021-01-06 15:08

    A would like to add additional information to explained answers

    Aggregation is a special form of Assosation (Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many)

    • Aggregation represents Has-A relationship.
    • It is a unidirectional association i.e. a one way relationship. For example, department can have employees but vice versa is not possible and thus unidirectional in nature.
    • In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity.

    Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.

    • It represents part-of relationship.
    • In composition, both the entities are dependent on each other.
    • When there is a composition between two entities, the composed object cannot exist without the other entity.

    Aggregation vs Composition

    - Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Department and Employee, delete the Department and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human.

    - Type of Relationship: Aggregation relation is “has-a” and composition is “part-of” relation.

    - Type of association: Composition is a strong Association whereas Aggregation is a weak Association.

    Reference: https://www.geeksforgeeks.org/association-composition-aggregation-java/

提交回复
热议问题