Difference between Association and Aggregation in Java


Association

Association in terms of objects refers to "has a" relationship between two related objects. For example, a employee has a communication address.

class Employee {
   String name;
   Address communicationAddress;
}
class Address {
   String address;
}

Aggregation

Aggregation in terms of objects refers to "has a"+ relationship between two related objects. For example, a department has multiple employees. It refers to having a collection of child objects in parent class. For example:

class Department {
   String name;
   List<Employee> employees;
}
class Employee {
   String name;
}
Sr. No.KeyAssociationAggregation
1DefinitionAssociation refers to "has a" relationship between two classes which use each other.Aggregation refers to "has a"+ relationship between two classes where one contains the collection of other class objects.
2FlexibilityInflexible in nature.Flexible in nature.
3LinkageLinkage is needed to maintain association.Linkage between objects in not mandatory.
4UMLLines are used to represent association.Diamond shape next to assembly class is used to represent the aggregation relationship.

Updated on: 28-Nov-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements