Do you think IIT Guwahati certified course can help you in your career?
No
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.
Association: Association defines the diversity between the objects.
Aggregation: Aggregation refers to the “has-a” relationship and is a particular case of Association.
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.
What is 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.
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
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.
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.
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.
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
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.
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.
What are the advantages of composition and aggregation?
Composition and aggregation offer key advantages in OOP: Composition ensures strong ownership and lifecycle management, where a part cannot exist independently of its whole. Aggregation allows for a more flexible relationship where the part can exist independently of the whole. Both promote modular, reusable, and maintainable code structures.
Conclusion
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.