Association, Composition and Aggregation in Java


Association refers to the relationship between multiple objects. It refers to how objects are related to each other and how they are using each other's functionality. Composition and aggregation are two types of association.


The composition is the strong type of association. An association is said to composition if an Object owns another object and another object cannot exist without the owner object. Consider the case of Human having a heart. Here Human object contains the heart and heart cannot exist without Human.


Aggregation is a weak association. An association is said to be aggregation if both Objects can exist independently. For example, a Team object and a Player object. The team contains multiple players but a player can exist without a team.

Example of Composition

//Car must have Engine
public class Car {
   //engine is a mandatory part of the car
   private final Engine engine;

   public Car () {
      engine = new Engine();

//Engine Object
class Engine {}

Example of Aggregation

public class Team {      
   //players can be 0 or more
   private List players;

   public Car () {
      players = new ArrayList();
//Player Object
class Player {}
Samual Sam
Samual Sam

Learning faster. Every day.