Association, Composition and Aggregation in Java

The things that we come across in our everyday lives are related to each other. A tree is related to the paper in the form of raw material; a shopkeeper is related to his customers, and so on. Let us now look at these from the aspect of programming. Each of the examples mentioned above denotes a separate class connected via objects.

In a program, most of the classes are interconnected. Mostly, these classes are related to each other. The relationship that exists among these classes is known as an association. This article will explain to you more about the association in Java.

What is Association in Java?

Association is a concept that plays a major role in an object-oriented programming language. It establishes the relationship between two separate classes. Association can be of one-to-one, one-to-many, many-to-one, and many-to-many. Let us look at each of these types in detail.

One-to-one: Some good real-time examples of the one-to-one association are a key for the car, a unique ID for a person, a passport, etc. There is a one-to-one relationship between the key and the car; the same goes with the other examples.

One-to-many: A single television used to watch multiple channels is a good real-time example for a one-to-many association.

Many-to-one: A tree can have many fruits in it. But the same fruit cannot be a part of another tree. This is a real-time example of a many-to-one association between a tree and a fruit.

Many-to-many: When the relationship between a teacher and students is considered, a teacher teaches for many students, and the students learn from many other teachers. Therefore, this relationship is said to be a many-to-many association.

Sample program to demonstrate the concept of Association:

import java.io.*;
class Library {
private String lib_name; // attribute1 of library class
Library(String lib _name) // Constructor
{
this.lib _name = lib _name;
}
public String getLibraryName()
{
return this.lib_name;
}
}

class Book {
private String book_name;
Book(String book_name)
{
this.book_name = book_name;
}
public String getBookName()
{
return this.book_name;
}
}

class FirstCode {

public static void main(String args[])
{
Library lib = new Library("Reader's Paradise");
Book bn = new Book("The Alchemist");
System.out.println(bn.getBookName() + "is available at " + lib.getLibraryName());
}
}

Output:

The Alchemist is available at Reader’s Paradise

Explanation:

The two classes, Library and Book, are associated through objects. As the class Library can have many books, the one-to-many relationship model exists.

Types of Java Association:

The concept of association in Java is further classified into two categories:

  • Composition
  • Aggregation

UML Notations of Association, Composition, and Aggregation in Java:

uml notations of association composition and aggregation in java

Aggregation in Java:

It is considered to be a weak form of association. When both the objects in an association can exist independently, it is said to be Aggregation. This means the destruction of an object will not affect the other.

For example, consider a wallet and money as two objects. A wallet can be without money, and money can exist without a wallet.

In Java, Aggregation allows only a one-to-one relationship and represents the Has-A relationship. The Aggregation is represented as a line with a diamond in a diagram.

Sample program for Aggregation in Java:

class Player {
int num;
String name; team_name;
Player(int num, String name, String team_name) {
this.num = num;
this.name = name;
this.team_name = team_name;
System.out.println("Player's name: " +name);
System.out.println("Player's ID: "+id);
Systemout.println("The Player is a member of team: "+team_name);
}
}

class Team {
int players;
String team;
Team(int players, String team){
this.players = players;
this.team = team_name;
}
}

class SportsClub{
String clubname;
int teamcount;
SportsClub(String name, int team_nos) {
this.clubname = name;
this.teamcount = team_nos;
PlayerDetails pd1 = new PlayerDetails(77, "Thomas", "Football");
PlayerDetails pd2 = new PlayerDetails(23, "Roger", "Hockey");
PlayerDetails pd3 = new PlayerDetails(17, "Mark", "Football");
}
}

Output:

Player’s name: Thomas
Player’s ID: 77
The player is a member of team: Football
Player’s name: Roger
Player’s ID: 23
The player is a member of team: Hockey
Player’s name: Mark
Player’s ID: 17
The player is a member of team: Football

Composition in Java:

Now, getting to the second type of association, the Composition is when the entities are dependent on each other. In Java, a composition has a one-to-many relationship. Here, both entities cannot function without the other.

It is a strong type of association. For instance, imagine a car having many machine parts. A car is nothing without mechanical parts. And on the other side, these parts have no proper value in existing independently.

Sample program on Composition in Java:

class Car {
public String machinepart;
public String noOfparts;
Car(String part, int num)
{
this.machinepart = part;
this.noOfparts = num;
}
}
class CarParts{
private final List parts;
CarParts(Listparts){
this.parts= parts;
}
public List getPartsCount(){
return parts;
}
}
public class FirstCode{
public static void main(String args[])
{
Part p1 = new Part("Engine", 1);
Part p2 = new Part("Brakes", 4);
Part p3 = new Part("Gears", 6);
List received = new ArrayList ();
parts.add(p1);
parts.add(p2);
parts.add(p3);

CarParts car_parts = new CarParts(received);
List part = car_parts.getPartsCount();
for (Parts parts: part){
System.out.println("The Count of" + part.machinepart + "is " + part.noOfparts);
}
}
}

Output:

The Count of Engine is 1

The Count of Brakes is 4

The Count of Rears is 6

Aggregation vs Composition in Java

Aggregation in Java Composition in Java
It is a weak form of Association This is a string form of Association
It represents a “has-A” relationship It represents a “part-of” relationship
The child can exist independently of the parent The child cannot exist independently
Code reusability is possible Code reusability is not possible
New associations can be created or reassigned to the existing class.   New associations cannot be created or reassigned.  

Conclusion:

Now, you might have a clear picture of what Association in Java is and how it works. To sum it up, the association is what lets a programmer establish a connection between two classes.

The two types of Association are Aggregation and Composition. In the case of Aggregation, the objects are independent and can exist without each other. Whereas in Composition, the objects are dependent on each other.

Leave a Reply

Your email address will not be published. Required fields are marked *