Java Association – Aggregation and Composition in Java

What if there are two classes that have some kind of relationship between them?

For example, the classes are Library and Books or Teacher and Student, don’t you think these two classes may be associated or connected with each other.

We can establish a relationship between them using Java Association. This relationship can be any type like one to one, one to many, many to one, or many to many.

Actually, these separate classes are connected with the help of their objects. In this article, we will discuss Association in Java, along with its types and implementation examples in Java.

We will learn each topic with a real-life example for your better understanding.

What is Association in Java?

Association in Java is one of the building blocks and the most basic concept of object-oriented programming. Association is a connection or relationship between two separate classes.

It shows how objects of two classes are associated with each other. The Association defines the multiplicity between objects. We can describe the Association as a has-a relationship between the classes.

Association is a kind of relationship between classes whose objects have an independent lifecycle and there is no ownership between the objects. It can be one-to-one, one-to-many, many-to-one, many-to-many.

Let’s take a real-life example to understand these types:

1. One-to-one

The best example of a one-to-one association is that one person or one individual can have only one passport. This is a one-to-one relationship between the person and the passport.

2. One-to-many

Suppose, there is a Doctor and his patients. So, one doctor is associated with many patients. So this is an example of a one-to-many Association between a doctor and patients.

3. Many-to-one

For example, there can be many books in one library, each book is associated with that library, and it can’t be a part of another library. So, many books are related to one library.

This is an example of a many-to-one Association between books and a library.

4. Many-to-many

If we talk about a teacher and student, there can be many students associated with one teacher, and also, the teacher can be related to many students.

So, the relationship between a teacher and student can be many-to-many.

Association in Java

Types of Java Association

There are two special forms of Association in Java. They are:
1. Composition
2. Aggregation

UML Notations of Association, Composition, and Aggregation in Java

There are different UML notations for aggregation, associations, and composition. We can easily differentiate them from these notations.

The following diagram shows the UML notations for each of them.

Java UML Notations

Example of Association in Java

package com.techvidvan.association;
class Person {
  String name;
  long id;
  Person(String name, long id) {
    this.name = name;
    this.id = id;
  }
}
class Passport extends Person {
  String personName;
  Passport(String name, long id) {
    super(name, id);
    this.personName = name;
  }
}
public class GovernmentAgency {
  public static void main(String args[]) {
    Passport obj = new Passport("Divya", 99884444);
    System.out.println(obj.personName + " is a person with a passport number: " + obj.id);
  }
}

Output:
Divya is a person with a passport number: 99884444

In the above example, there is a one to one association between two classes: Person and Passport. Both the classes represent two separate entities.

Aggregation in Java

Aggregation in Java is a special kind of association. It represents the Has-A relationship between classes. Java Aggregation allows only one-to-one relationships.

If an object is destroyed, it will not affect the other object, i.e., both objects can work independently.

Let’s take an example. There is an Employee in a company who belongs to a particular Department. If the Employee object gets destroyed still the Department can work independently.

The end of the Employee object will not affect or destroy the Department object. The Aggregation is represented as a line with a diamond.

Here the Department has-a Employee and therefore both are in the Aggregation with each other.

Aggregation in Java

package com.techvidvan.association;
class Employee {
  int id;
  String name;
  String dept;
  Employee(int id, String name, String dept) {
    this.id = id;
    this.name = name;
    this.dept = dept;
    System.out.println("\nEmployee name is " + name);
    System.out.println("Employee Id is " + id);
    System.out.println("Employee belongs to the " + dept + " Department");
  }
}
class Department {
  String deptName;
  int noOfemployees;
  Department(String name, int numberOfemployees) {
    this.deptName = name;
    this.noOfemployees = numberOfemployees;
  }
}
class University {
  String universityName;
  int noOfdepartments;
  University(String name, int departments) {
    this.universityName = name;
    this.noOfdepartments = departments;
  }
}
public class AggregationDemo {
  public static void main(String[] args) {
    Employee e1 = new Employee(101, "Rishi", "Engineering");
    Employee e2 = new Employee(167, "Rohan", "Management");
    Employee e3 = new Employee(125, "Sneha", "Accounts");
  }
}

Output:
Employee name is Rishi
Employee Id is 101
Employee belongs to the Engineering Department

Employee name is Rohan
Employee Id is 167
Employee belongs to the Management Department

Employee name is Sneha
Employee Id is 125
Employee belongs to the Accounts Department

Composition in Java

The composition is another form of aggregation which is considered as the restricted form of Association.

In this type of association, the entities are completely dependent on each other, unlike the aggregation. Composition allows for one-to-many relationships between objects.

It represents a part-of relationship between two objects. One entity cannot exist without the other. Composition in Java represents a one-to-many relationship.

Suppose, there is a House and inside the house, there are many rooms. We consider the relationship between the house and the rooms.

A single house can have multiple rooms but a single room can not have multiple houses. And, if we delete the house, the rooms will automatically be deleted.

So the two entities: house and rooms, are dependent on each other. Room is a part of the House and they two hold a relation of composition.

Composition in Java

package com.techvidvan.association;
import java.util. * ;
class Room {
  public String roomName;
  public int roomNo;
  Room(String name, int number) {
    this.roomName = name;
    this.roomNo = number;
  }
}
class House {
  private final List < Room > rooms;
  House(List < Room > rooms) {
    this.rooms = rooms;
  }
  public List < Room > getTotalRoomsInHouse() {
    return rooms;
  }
}
public class CompositionDemo {
  public static void main(String[] args) {
    Room room1 = new Room("Dining Room", 2);
    Room room2 = new Room("Bed Room", 5);
    Room room3 = new Room("Living Room", 3);
    List < Room > books = new ArrayList < Room > ();
    books.add(room1);
    books.add(room2);
    books.add(room3);
    House house = new House(books);
    List < Room > rooms = house.getTotalRoomsInHouse();
    for (Room room: rooms) {
      System.out.println("The Room Number of " + room.roomName + " is: " + room.roomNo);
    }
  }
}

Output:
The Room Number of Dining Room is: 2
The Room Number of Bed Room is: 5
The Room Number of Living Room is: 3

Conclusion

In this article, we discussed Association in Java. If we want to establish connections between two classes then we can use any type of association according to the need.

If the objects are independent of each other, we use Aggregation and if they are dependent then we use Composition.

In Aggregation, if one object dies, the other still remains alive but in Composition, if one dies then the other automatically dies.

We covered everything with real-life so that you can connect it in a better way. We hope you enjoyed reading this article.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.