Association vs Aggregation vs Composition in Java

In this tutorial, we will differentiate the main points among association vs aggregation vs composition in java with example programs.

The similarity among Association, aggregation, and composition are that they are core concepts of OOPs and represent Has-A relationship between two classes.

Now, we will discuss the main difference among them. So, let’s start with association first.

Association

1. Association in Java is a relationship that defines the relationship between two classes that are independent of one another.

2. Association has no owner relationship. For example, multiple students can connect with a single teacher and a single student can connect with multiple teachers but there is no ownership between objects.

Both teacher and student have their own life cycle. Both can connect and destroy independently.

3. It can define several relationships such as one-to-one, one-to-many, many-to-one, and many-to-many.

4. Association is the combination of aggregation and composition.

Let’s take an example program based on Java association.

Program source code 1:

public class Person {
 private String name;
 public String getName() {  
     return name;  
 }  
 public void setName(String name) {  
     this.name = name;  
 }  
}
public class Address {
 private String state;
 private String city;
 private String zip;
 
public String getState(){
 return state;	
 }
public String getCity(){
 return city;
 }
public String getZip(){
 return zip;	
 }
public void setState(String state){
  this.state = state;
}
public void setCity(String city){
  this.city = city;
 }
public void setZip(String zip){
  this.zip = zip;
 }
}
public class OneToOneTest {
public static void main(String[] args) {
 Person p1 = new Person();
  p1.setName("John");
 
Person p2 = new Person();
 p2.setName("Shubh");
 
Address a1 = new Address();
 a1.setState("Jharkhand");
 a1.setCity("Dhanbad");
 a1.setZip("123524");  
 
Address a2 = new Address();
 a2.setState("Maharashtra");
 a2.setCity("Mumbai");
 a2.setZip("123635");  
 
// Association between two classes in the main method.
  System.out.println(p1.getName()+ " lives at address " +a1.getCity()+ "' " +a1.getState()+ ", " +a1.getZip()+ " but he has also address at " +a2.getCity()+ ", " +a2.getState()+ ", "+a2.getZip());
  System.out.println(p2.getName()+ " lives at address " +a2.getCity()+ "' " +a2.getState()+ ", " +a2.getZip()+ " but she has also address at " +a1.getCity()+ ", " +a1.getState()+ ", "+a1.getZip());
 
  }
}
Output:
      John lives at address Dhanbad' Jharkhand, 123524 but he has also address at Mumbai, Maharashtra, 123635
      Shubh lives at address Mumbai' Maharashtra, 123635 but she has also address at Dhanbad, Jharkhand, 123524

Aggregation

1. An aggregation is a special form of unidirectional association that represents an ownership relationship between two objects.

2. In aggregation, two aggregated objects have their own life cycles but one of the objects is the owner of the Has-A relationship.

For example, the association between a car and the passengers in the car is aggregation. Passengers can be the owner of car. Both have their own life cycle but one of the passengers can be the owner of car.

3. Aggregation represents only a unidirectional relationship.

Let’s take an example program based on Java aggregation.

A most common example of aggregating relationship is “A student has an address”. A student has many pieces of information such as name, roll no, email id, etc.

It also contains one more important object named “address” that contains information such as city, state, country, zip code. Look at the program source code to understand aggregation.


Program source code 2:

public class Address {
String city, state, country; 
int pinCode;

public Address(String city, String state, String country, int pinCode) {  
  this.city = city;  
  this.state = state;  
  this.country = country;  
  this.pinCode = pinCode;	    
  }  
}
A student Has-A an address. Therefore, the Student class must be able to receive address information as follows:

public class Student {  
 String name; 
 int rollNo;
 Address address;
 int pinCode;
	  
public Student(String name, int rollNo, Address address) {  
 this.rollNo = rollNo;  
 this.name = name;  
 this.address=address; 
}  
void display(){  
 System.out.println("Name: " +name + ", "+"Roll no: " +rollNo);
 System.out.println("Address:");
 System.out.println(address.city+" "+address.state+" "+address.country+ " " +address.pinCode); 
 System.out.println("\n");
}  
public static void main(String[] args) 
{  
 Address addr1 = new Address("Dhanbad,","Jharkhand,","India,", 826001);  
 Address addr2 = new Address("Ranchi,","Jharkhand,","India,", 825001);  
	  
 Student st1 = new Student("Deep", 05, addr1);  
 Student st2 = new Student("John", 02, addr2);  
	     
   st1.display();  
   st2.display();  
 }  
}
Output:
       Name: Deep, Roll no: 5
       Address:
       Dhanbad, Jharkhand, India, 826001

       Name: John, Roll no: 2
       Address:
       Ranchi, Jharkhand, India, 825001

Composition

1. A composition is a special and more restrictive form of aggregation where an object cannot exist on its own. The “whole” is actually dependent on the “part”.

For example, the association between a car and its engine represents composition. Since a car cannot exist without an engine.

2. In composition, both the entities are associated with each other and cannot exist on their own.

3. It represents an exclusive whole-part relationship and controls the lifetime of objects.

Let’s take an example program based on composition.

Program source code 3:

public class Engine {  
 private String type;
 private int horsePower;
 
Engine(String type, int horsePower){
 this.type = type;
 this.horsePower = horsePower;
}
public String getType(){
  return type;	
}
public int getHorsePower(){
  return horsePower;	
 }
public void setType(String type){
  this.type = type;	
 }
public void setHorsePower(int horsePower){
  this.horsePower = horsePower;	
 }
}
public class Car 
{
 private final String name;
 private final Engine engine; // Composition.
 
public Car(String name, Engine engine){
 this.name = name;	
 this.engine = engine;
}
public String getName(){
 return name;	
 }
public Engine getEngine(){
  return engine;	
 }
}
public class Test {
public static void main(String[] args) 
{
// Creating an object of Engine class.
   Engine engn = new Engine("Petrol", 300);

// Creating an object of Car class.
  Car car = new Car("Alto", engn);
  System.out.println("Name of car: " +car.getName()+ "\n" +"Type of engine: " +engn.getType()+ "\n" + "Horse power of Engine: " +engn.getHorsePower());
}
}
Output:
      Name of car: Alto
      Type of engine: Petrol
      Horse power of Engine: 300

Difference between Association, Aggregation, and Composition


List of key points to differentiate between association, aggregation, and composition in tabular form:

Association
Aggregation
Composition
1. Association established the relation between two classes that is independent of each another. 1. Aggregation defines a special form of unidirectional association between two classes. 1. Composition represents a special and more restrictive form of aggregation where an object cannot exist on its own.
2. In association, there is no owner relationship. 2. In aggregation, one of the objects is the owner of the Has-A relationship. 2. In composition, both the entities are associated with each other and cannot exist on their own.
3. Association defines the relationships as one-to-one, one-to-many, many-to-one, and many-to-many. 3. It defines only a unidirectional relationship. 3. It represents an exclusive whole-part relationship.

Hope that this tutorial has covered all the important points related to differentiating among association vs aggregation vs composition in java. I hope that you will have understood the basic difference between them.

If the technical interviewer asks the difference between association, aggregation, and composition, three to four points are sufficient for answer.
Thanks for reading!!!
Next ⇒ Super Keyword in Java
⇐ Prev Next ⇒