Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Association in Java
One-to-one association 
One-to-many association 
Many-to-one association 
Many-to-many association 
Aggregation in Java
Composition in Java
Difference between association, aggregation, composition in Java
Frequently Asked Questions
What is the difference between Association and Aggregation?
What is the difference between Association and Composition?
What is Aggregation and composition in Java with real life example?
Last Updated: Mar 27, 2024

Association, Composition and Aggregation in Java

Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM
Java Association, Aggregation and Composition


We know that in Java, most of the things revolve around “Objects and Classes.” The objects and classes have various kinds of relationships that help us design software applications. The multiple relationships that exist in Java are based on Association, Aggregation, and Composition. In an object-oriented programming language, we would probably land on problems containing objects of different classes. We could explain this by stating the various relationships the classes have with each other. Association, Aggregation, and Composition all refer to these problems and have minor differences with each other. Before diving into the article, let’s understand what these terms mean.

  1. Association: Association defines the diversity between the objects.
  2. Aggregation: Aggregation refers to the “has-a” relationship and is a particular case of Association.
  3. Composition: Composition is a restricted type of Aggregation. This means that the entities are highly dependent on each other.4

To understand a bit more about the concepts, let’s consider each of the aspects and study it in detail with a few examples.

Association in Java

Association is a concept that establishes a relationship between two separate classes through their objects. An association relationship can be of 4 different types- 

  • one-to-one association
  • one-to-many association
  • many-to-one association
  • many-to-many association

Association is one of the building blocks of Object-Oriented Programming. Association describes the relationship between the two classes.

To understand the different types of relationships, let’s consider a few real-life examples.

One-to-one association 

A person can have only one Aadhar Card. This defines the one-to-one relationship between the two entities.

One-to-many association 

A teacher explains the concept to many students in a classroom. This defines one-to-many associations between the teacher and the students.

Many-to-one association 

In an office, many different types of departments are related to an employee. For example, an employee has to report to the financing department for his salary, and he has to report to his manager about the report, the employee is related to the head for any reviews in his project. This is an example of many-to-one associations, where there are many departments related to the employee.

Many-to-many association 

In a cinema hall, the projected movie is meant for a wider audience and not just for one person. There are many actors in the film portraying their messages to many people through the movie. This is an example of many-to-many associations, where many different actors are related to many other people.

Association in java

Let’s consider a small code to understand the concept better,

class Person {
  String name;
  double id;
  //constructor for defining the class objects
  Person(String name, double id2) { = name; = id2;
class AadharCard extends Person {
  String personName;
  AadharCard(String name, double id) {
    super(name, id);
    this.personName = name;
public class AadharBank {
  public static void main(String args[]) {
    AadharCard obj = new AadharCard("Ninja", 981049321);
    System.out.println(obj.personName + " is a person with an Aadhar number: " +;



Ninja is a person with an Aadhar number: 981049321

Practice by yourself on java online compiler.
The above example represents the one-to-one relationship between the Person class and the AadharCard class. Both classes represent different entities. 

Also see, Access modifiers in java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Aggregation in Java

Aggregation is a particular type of Association. It represents the has-a relationship between the two classes. The relationship between the two classes is entirely independent, which means if one of the objects of the class gets deleted, it won’t affect the other.

The Aggregate class contains a reference to one more class and is said to be responsible for the class. Each class that is referred to is viewed as a piece of the Aggregate class.

Let’s take an easy example. A student attends a school. After the completion of his studies, he can quickly leave school. That means the end of the student object would not destroy the School object. We can say that the Student ‘has-a’ relationship with the school.

Aggregation in java

To understand the concept better let’s go through the program once.

class Student {
  int id;
  String name;
  String school_name;
  Student(int id, String name, String school_name) { = id; = name;
    this.school_name = school_name;
    System.out.println("\nStudent name is " + name);
    System.out.println("Student Id is " + id);
    System.out.println("Student belongs to the " + school_name + "School");
class school {
  String schoolName;
  int noOfStudents;
  school(String name, int numberOfStudents) {
    this.schoolName = name;
    this.noOfStudents = numberOfStudents;
public class AggregationClass {
  public static void main(String[] args) {
    Student n1 = new Student(1, "NinjaA", "Coding Ninjas");
    Student n2 = new Student(2, "NinjaB", "Code");
    Student n3 = new Student(3, "NinjaC", "XYZ");



Student name is NinjaA
Student Id is 1
Student belongs to the Coding Ninjas School

Student name is NinjaB
Student Id is 2
Student belongs to the Code School

Student name is NinjaC
Student Id is 3
Student belongs to the XYZ School

In the above program, the school and the student represent two individual entities related to each other by a ‘has-a’ relationship. When an object from the student class is deleted, it does not change the object of the School Class. Hence, even though the classes are connected, there is no interdependence between them, as we saw in Association.
We, therefore, say that the Aggregation is a special case of Association.


You can also check out Has-A Relationship in Java here.

Composition in Java

Composition in Java


We have just learned about Aggregation as a Special case of Association, and now when we talk about Composition, it is said that Composition is nothing but a restricted type of Aggregation. Let’s have a look at the definition of the concept. Composition in java is a concept which states that there lies a strong relationship between the two objects associated with the given two classes. It is also said to fulfill the “has-a” relationship between the two classes. In composition, the parent class owns the child class which means the child class cannot be a stand-alone entity. We use Composition mainly because it allows us to have Multiple Inheritance in Java, also it will enable us to reuse the code.

Let’s understand this by using a real-life example,

A car needs oil to run, and we know that our car would not be able to start without filling up the gas tank. So this means that even though the car would be useless without the Oil Class, we can say that car and oil have a “has-a” relationship.

The implementation of Composition is given below.

class CarOil {
  public void FillOil() {
    System.out.println("The fuel is full in the car");
  public void EmptyOil() {
    System.out.println("The car has low oil");
class Car {
  private String colour;
  private int maxi_Speed;
  public void carDetails(){
    System.out.println("Car Colour= "+colour + ", Maximum Speed= " + maxi_Speed);
  //Setting colour of the car
  public void setColour(String colour) {
    this.colour = colour;
  //Setting maximum car Speed
  public void setMaxiSpeed(int maxi_Speed) {
    this.maxi_Speed = maxi_Speed;
class Ninja extends Car {
  public void NinjaOil() {
    CarOil Ninja_Oil = new CarOil(); //composition
public class Main {
  public static void main(String[] args) {   
    Ninja NinjaCar = new Ninja();



Car Colour= Orange, Maximum Speed= 180
The fuel is full in the car

In this program, the class Ninja is a car that is extended from the Class Car. The CarOil is used in the Ninja class.

Composition offers adaptability and vigorous code. Its code reusability helps in keeping away from code duplication and accomplishing cost-viability, which makes it one of the broadly utilized strategies in different projects.

Also see, Duck Number in Java

Difference between association, aggregation, composition in Java

Here's a table summarizing the differences between association, aggregation, and composition in Java:

Aspect Association Aggregation Composition
Dependency Strength Weak Weak Strong
Ownership None None Ownership
Lifecycle Independent Independent Contained object's lifecycle managed
Multiplicity One-to-one, one-to-many, many-to-many One-to-one, one-to-many, many-to-many Typically one-to-one or one-to-many
UML Representation Dashed line with an arrowhead Dashed line with an empty diamond Solid line with a filled diamond
Example Student and School Department and University Car and Engine

Frequently Asked Questions

What is the difference between Association and Aggregation?

Association means the relationship between the two interdependent classes. The connection is established using the two objects. Through association, a class can use another class component. Whereas, Aggregation is a particular case of Association in which the two classes are related to each other, and they follow a whole and part relationship.

What is the difference between Association and Composition?

Association means to specify a relationship between two classes using their objects. It allows multiple types of relationships like one-to-one, one-to-many, many-to-one, and many-to-many. In contrast, Composition is a restricted Aggregation, which means as per aggregation, it allows the relationship between the two classes, but the objects are dependent on each other. In Composition, only a one-to-one relationship is valid.

What is Aggregation and composition in Java with real life example?

In Java, aggregation represents a "has-a" relationship, where one class contains another class as part of its structure, but they can exist independently. Example: A university "has" departments. Composition is a stronger relationship, where one class owns another class, and the owned class cannot exist independently. Example: A car "has" an engine.


In the above article, we learned about the key terms in Object-Oriented Programming: Association, Aggregation, and Composition.
Association means to have a has-a relationship between the two classes, which are specified by their Objects. Association is used to establish a relationship between the two classes through their objects.
Aggregation is just a particular type of Association that offers only one-to-one relationships. In Aggregation, the objects are independent of each other, which means that they can exist independently.
Composition, on the other hand, is a restricted Aggregation. It also offers only a one-to-one relationship between the two classes, but the objects in Composition depend on each other. They hence cannot exist solely(without each other). The article discussed each topic with easy examples. To learn more about Java as an Object-Oriented Language, do give this blog a read.

Difference Between Rank and Dense Rank

Visit here to learn more about Java. You can also practice problems on Coding Ninjas Studio. If you liked this blog, share it with your friends.

Previous article
What’s the Difference Between Iterator and ListIterator?
Next article
How to fix NoSuchElementException in Java?
Live masterclass