Understanding Association, Aggregation, and Composition in Java

Understanding Association, Aggregation, and Composition in Java
Understanding Association, Aggregation, and Composition in Java

Introduction

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

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.

  1. One-to-one association: A person can have only one Aadhar Card. This defines the one-to-one relationship between the two entities.
  2. 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.
  1. 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.
  2. 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.
types_of_association

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) {
    this.name = name;
    this.id = 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: " + obj.id);
  }
}
OUTPUT

Ninja is a person with an Aadhar number: 981049321

The above example represents the one-to-one relationship between the Person class and the AadharCard class. Both classes represent different entities. 

Aggregation

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

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) { 
    this.id = id;
    this.name = 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");
  }
}
OUTPUT

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.

Composition

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 inheritances, 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
    Ninja_Oil.FillOil();
  }
} 
 
public class Main {
  public static void main(String[] args) {   
    Ninja NinjaCar = new Ninja();
    NinjaCar.setColour("Orange");
    NinjaCar.setMaxiSpeed(180);
    NinjaCar.carDetails();
    NinjaCar.NinjaOil();
  }
}
OUTPUT

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.

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 use of Aggregation and Composition in Java?

Aggregation is used when the objects are independent, and Composition is used when the objects are dependent on each other. Aggregation is used for code reusability, and Composition offers adaptability and vigorous code.

How can you define Aggregation in Java with an example?

Aggregation is a specialized unidirectional association that represents the ownership relation between the two classes. Aggregation only allows one-to-one relationships. In aggregation, the two objects are independent, even though they share the same classes, but their existence is independent. Let’s take an example to understand better- A citizen and a country have an aggregation relationship. Where the citizen shares the government-specified documents, but it is not reliable just on one particular government. If the citizen reallocates, the government can survive as a whole. Hence, we can say that they do share a relationship but do not depend on each other as a whole. These types of relationships are termed whole and part relationships.

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.

Key Takeaways

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.

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

By: Rubleen Kaur