Java Association | Java Composition | Java Aggregation

Java Composition Java Aggregation Java Association

Java Association | Java Composition | Java Aggregation

Composition and Aggregation in Java programming basically are used to represent the Association between two objects. And the relationship between two objects is represented by Association. These terms denote how two objects are dependent on each other. In this tutorial, we will discuss about the terms Composition and Aggregation in java. What is the purpose of using them and how to implement in java programming?


Java Composition Java Aggregation Java Association
Java Composition Java Aggregation Java Association


Java Composition

Let’s first understand, what is Composition in java. Before we jump directly on the definition, let’s take a real world example of composition :

House is the perfect example of composition. House is built up of bricks, woods, glass and much more. If we destroy the house then bricks, wood, glass and other objects are meaningless. House is dependent on these objects. So composition is when different objects compose together to create a meaningful object.

We can take example of bike, bike has different components like tyres, breaks, head light, engine etc. They are meaningless if you remove all of them.

 
In terms of Java, Composition is the association between two objects – when Object A is directly dependent on Object B and Object A can not meaningfully exist without Object B.

Composition is also denoted by “part-of” relationship. Object A is part of Object B.

Example

class Bike {
      private final Engine engine;
      private final HeadLight headLight;
      private final DashBoard dashBoard;
}

class Engine { }
class HeadLight { }
class DashBoard { }

In above example- Engine, HeadLight, DashBoard classes are the part of Class Bike and without Bike class they are meaningless.

We need to use term “final” (We will discuss final variable later. But for now, final variable much be initialized) which will ensure that the parts of the Bike must be available in order to function. This also represents strong association between objects.

Java Aggregation

Let’s understand the concept Aggregation with real world example.

Let’s take an example of Company and employee. Employees go to the company daily. Employees do their job there and leave the company when time is over. Company has employees to do their job. When you leave company, you and company both can exist separately. This is aggregation.

When a object doesn’t need to be dependent upon another object to meaningfully exist is called Aggregation. Aggregation is represented by “has” relationship.

Example

class Company {
      private Employee employe;
}

class Employee { }

In the above example, Company “has” employees to do their jobs.

So at the end, Association, Composition and Aggregation are as follows :

  • Association is the relationship between two objects. Association can be one to one, one to many, many to one and many to many.
  • Composition is the association when one object is the part of another object. Like Bike and other parts.
  • Aggregation is the association when one object has another object. Like Company and employee.

Leave a Reply

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