Difference between Association and Aggregation in Java

Share

Association

It can be defined as the relationship where two different classes communicate with each other for some purpose. Any kind of relationship if exists between two classes then they are said to be associated. The lifetime of an object of a class is not dependent on any other class’s object. It can be a bi-directional relationship.

Let’s look at an example of implementing an association in Java.

Program:

class School
{
private String name; 
School(String name) 
{ this.name = name; } 
public String getSchool() 
{ return this.name; } 
}   

class Student 
{ 
private String name; 
Student(String name)  
{ this.name = name; } 
public String getStudent() 
{ return this.name; }  
} 
 
public Class Example
{ 
public static void main (String[] args)  
{ 
School sc = new School("A"); 
Student st = new Student("xxx"); 
          
System.out.println(emp.getStudent() + " is student of " + bank.getSchool()); 
} 
}

Can an aggregation be an association?

Yes. Every aggregation is an association.

Aggregation

Basically, aggregation is a type of association but in aggregation, the relationship between two classes is called Has-A relationship. It is a unidirectional relationship. In this, both the classes can exist independently i.e. if one of the class doesn’t exist that will not affect the other class. In aggregation one class is the owner and one is the part class. The part only has a reference variable in the owner class.

Let’s look at an example of implementing an Aggregation in Java.

Program:

public class Address {  
String add;  
public Address(String add) {  
this.add = add; 
}  
}

public class Emp {  
int id;  
Address address;  
public Emp(int id, Address address) {  
this.id = id;   
this.address=address;  
}  
void display(){  
System.out.println(id);  
System.out.println(address.add);  
}  

public static void main(String[] args) {  
Address a1=new Address(“Delhi”);  
Address a2=new Address(“UP”);  
Emp e=new Emp(1, a1);  
Emp e2=new Emp(2, a2);  
e.display();  
e2.display();  
}  
}

 

In the above example, we have two classes Address and Emp. Emp has a reference variable of Address class address. The address in “Emp” class is dependent on the add from Address class.

Difference between Association and Aggregation in Java

  • In association, both the classes are independent of each other. In aggregation, one class is a part of another class
  • Association has a bi-directional relationship, whereas aggregation has a unidirectional relationship between the classes.
  • All aggregation relationships are also association but not all associations are called aggregation relation.
  • Example of association is School and Student where both can exist without each other. Example of aggregation is Employee and Address where Address is a part of Employee class.

Key Differences:

AssociationAggregation
Classes are independent from one anotherOne class is a part of another class, It can't exist independently
It  has a bi-directional relationshipIt has a unidirectional relationship between the classes
A line is used to represent AssociationA line with diamond shape is used to represent Aggregation
School and Student where both can exist without each otherEmployee and Address where Address is a part of Employee class

 

 

This post was last modified on June 20, 2020

Abhishek

Published by
Abhishek
Tags: Aggregation Association Association and Aggregation Difference Difference between Association and Aggregation in Java