Association in Java

Association in Java defines the connection between two classes that are set up through their objects. Association manages one-to-one, one-to-many, and many-to-many relationships. In Java, the multiplicity between objects is defined by the Association. It shows how objects communicate with each other and how they use the functionality and services provided by that communicated object. Association manages one-to-one, one-to-many, many-to-one and many-to-many relationships.

Association in Java

Let's take an example of each relationship to manage by the Association.

  1. A person can have only one passport. It defines the one-to-one
  2. If we talk about the Association between a College and Student, a College can have many students. It defines the one-to-many
  3. A state can have several cities, and those cities are related to that single state. It defines the many-to-one
  4. A single student can associate with multiple teachers, and multiple students can also be associated with a single teacher. Both are created or deleted independently, so it defines the many-to-many

Let's takes an example of an Association to understand how it works in Java.

AssociationExample.java

Output:

Association in Java

Description

In the above example, we create two separate classes, i.e., Person and Mobile, associated through their objects. A person can have many mobile numbers, so it defines the one-to-many relationship.

Types of Association

In Java, two types of Association are possible:

  1. IS-A Association
  2. HAS-A Association
    1. Aggregation
    2. Composition
Association in Java

1) IS-A Association

The IS-A Association is also referred to as Inheritance. We all know about Inheritance in Java and if you don't know about it, click here.

2) HAS-A Association

The HAS-A Association is further classified into two parts, i.e., Aggregation and Composition. Let's understand the difference between both of them one by one.

1) Aggregation

In Java, the Aggregation association defines the HAS-A relationship. Aggregation follows the one-to-one or one-way relationship. If two entities are in the aggregation composition, and one entity fails due to some error, it will not affect the other entity.

Let's take the example of a toy and its battery. The battery belongs to a toy, and if the toy breaks and deletes from our database, the battery will still remaining in our database, and it may still be working. So in Aggregation, objects always have their own lifecycles when the ownership exists there.

Association in Java

AggregationExample

Output:

Association in Java

Description:

In the above example, there is a college that has several courses like BSC-CS, MCA, and Poly. Every course has several students, so we make a College class that has a reference to the object or list of objects of the Course class. That means College class is associated with Course class through the objects. Course class also has a reference to the object or list of objects of Student class means it is associated with Student class through its object and defines the HAS-A relationship.

Association in Java

2) Composition

A restricted form of the Aggregation where the entities are strongly dependent on each other. Unlike Aggregation, Composition represents the part-of relationship. When there is an aggregation between two entities, the aggregate object can exist without the other entity, but in the case of Composition, the composed object can't exist. To learn more about Composition, click here.

Let's take an example to understand the concept of Composition.

We create a class Mobile that contains variables, i.e., name, ram and rom. We also create a class MobileStore that has a reference to refer to the list of mobiles. A mobile store can have more than one mobile. So, if a mobile store is destroyed, then all mobiles within that particular mobile store will also be destroyed because mobiles cannot exist without a mobile store. The relationship between the mobile store and mobiles is Composition.

CompositionExample.java:

Output:

Association in Java