Association In Java: Composition and Aggregation

1. Objective

In our last tutorial, we discussed Java Copy Constructor. Now, we are going to study about the association in Java and its composition and aggregation in Java. Moreover, we will look at how to implement association in Java. Along with this, we will see Java association class example. At the end of this article, we will know the difference between association and aggregation in Java and also composition in Java.

So, let us start Association, Aggregation and Composition in Java code.

Association in Java

Association in Java: Composition and Aggregation in Java

2. What is the Association In Java?

Basically, Association in Java is a connection between two separate classes which sets up through their Objects. Although, Java association can balance, one-to-many, many-to-one, and many-to-many.

Let’s see how to implement association in Java. In Object-Oriented programming, an Object imparts to other Object to utilize usefulness and administrations gave by that question.

Do you know What is Java Class and Object?

association in Java

Association in Java

Java Association Class Example –

import java.io.*;
class Bank
{
   private String name;
   // bank name
   Bank(String name)
   {
       this.name = name;
   }
   public String getBankName()
   {
       return this.name;
   }
}
class Employee
{
   private String name;
   Employee(String name)
   {
       this.name = name;
   }
   public String getEmployeeName()
   {
       return this.name;
   }
}
class Association
{
   public static void main (String[] args)
   {
       Bank bank = new Bank("HDFC");
       Employee emp = new Employee("anshul");
       System.out.println(emp.getEmployeeName() +
              " is employee of " + bank.getBankName());
   }
}

"<yoastmark

Output:
Anshul is an employee of HDFC

So, in the above example of Association in Java, the bank has many employees and it is connected to various objects. Thus, this is a one-to-many relationship.

Read about Java Method – Declaration and Calling a Java Method

3. Types of Java Association

The two types of association in Java Programming Language are –

a. Aggregation in Java

Now, Aggregation in Java is a special type of association. Java Aggregation has the following characteristics –

  • A has-A relationship is represented here.
  • It is a one-way relationship, i.e. a unidirectional relationship.
  • Ending one entity won’t affect another, both can be present independently.
import java.io.*;
import java.util.*;
class Student
{
   String name;
   int id ;
   String dept;
   Student(String name, int id, String dept)
   {
       this.name = name;
       this.id = id;
       this.dept = dept;
   }
}
class Department
{
   String name;
   private List<Student> students;
   Department(String name, List<Student> students)
   {
       this.name = name;
       this.students = students;
   }
   public List<Student> getStudents()
   {
       return students;
   }
}
class Institute
{
   String instituteName;
   private List<Department> departments;
   Institute(String instituteName, List<Department> departments)
   {
       this.instituteName = instituteName;
       this.departments = departments;
   }
   public int getTotalStudentsInInstitute()
   {
       int noOfStudents = 0;
       List<Student> students;
       for(Department dept : departments)
       {
           students = dept.getStudents();
           for(Student s : students)
           {
               noOfStudents++;
           }
       }
       return noOfStudents;
   }
}
class GFG
{
   public static void main (String[] args)
   {
       Student s1 = new Student("Mia", 1, "CSE");
       Student s2 = new Student("Priya", 2, "CSE");
       Student s3 = new Student("John", 1, "EE");
       Student s4 = new Student("Rahul", 2, "EE");
       List <Student> cse_students = new ArrayList<Student>();
       cse_students.add(s1);
       cse_students.add(s2);
       List <Student> ee_students = new ArrayList<Student>();
       ee_students.add(s3);
       ee_students.add(s4);
       Department CSE = new Department("CSE", cse_students);
       Department EE = new Department("EE", ee_students);
       List <Department> departments = new ArrayList<Department>();
       departments.add(CSE);
       departments.add(EE);
       Institute institute = new Institute("BITS", departments);
       System.out.print("Total students in institute: ");
       System.out.print(institute.getTotalStudentsInInstitute());
   }
}

Output:
So, total students in the institute: 4

So, in this case, there is an Institute which has number of offices like CSE, EE. Each office has number of understudies. In this way, we influence an Institute to the class which has a reference to Object or number of Objects (i.e. Rundown of Objects) of the Department class. That implies Institute class is related to Department class through its Object(s). What’s more, Department class has likewise a reference to Object or Objects (i.e. Rundown of Objects) of Student class implies it is related with Student class through its Object(s). It speaks to a Has-A relationship. Code rescue can be best achieved by Aggregation in Java.

Let’s Learn Interface in Java with Example program

association in Java

Association in Java- Aggregation Example

b. Composition in Java

This is a restricted form of Java aggregation that is the quantities are highly dependent on each other. Java composition represents a part-of relationship. One entity cannot exist without the other.

import java.io.*;
import java.util.*;
class Book
{
   public String title;
   public String author;
   Book(String title, String author)
   {
       this.title = title;
       this.author = author;
   }
}
class Library
{
   private final List<Book> books;
   Library (List<Book> books)
   {
       this.books = books;
   }
   public List<Book> getTotalBooksInLibrary(){
     return books;
   }
}
class GFG
{
   public static void main (String[] args)
   {
       Book b1 = new Book("Jane eyre", "Charolate Bronte");
       Book b2 = new Book("Wuthering heights", "Emily Bronte");
       Book b3 = new Book("Emma", "Jane");
       List<Book> books = new ArrayList<Book>();
       books.add(b1);
       books.add(b2);
       books.add(b3);
       Library library = new Library(books);
       List<Book> bks = library.getTotalBooksInLibrary();
       for(Book bk : bks){
           System.out.println("Title : " + bk.title + " and "
           +" Author : " + bk.author);
       }
   }
}

Output

The Title: Jane Eyre and  Author: Charolate Bronte

Title: Wuthering heights and  Author:  Emily Bronte

Title: Emma and  Author: Jane

Follow this Link to know about Packages in Java

4. Aggregation vs Composition in Java

  • In Java aggregation, a child can be present independently while in composition it cannot.
  • While aggregation has-A relationship, a composition has a part-of relationship.
  • Aggregation is weak Association while composition being a strong one.
import java.io.*;
class Engine
{
       public void work()
   {
       System.out.println("Engine of car has been started ");
   }
}
final class Car
{
   private final Engine engine; // Composition
   Car(Engine engine)
   {
       this.engine = engine;
   }
   // car start moving by starting engine
   public void move()
   {
       {
          engine.work();
           System.out.println("Car is moving ");
       }
   }
}
class GFG
{
   public static void main (String[] args)
   {
       Engine engine = new Engine();
       Car car = new Car(engine);
       car.move();
   }
}

Output:

The engine of a car has been started

The car is moving.

Check Method Overloading vs Overriding in Java

So, this was all about Association in Java and its types i.e. Aggregation and Composition in Java. Hope you like our explanation.

5. Conclusion

Hence, in this Java Association tutorial, we learned about Association and its types composition and aggregation. Moreover, we saw an example of Association in Java. Also, in Java Composition, we discussed characteristics and composition example in Java. Lastly, we saw Aggregation vs Composition in Java. Furthermore, if you have any query, feel free to ask in the comment section.

Related Topic- Garbage Collection in Java – Java GC Collect

For reference

1 Response

  1. Shravya Kodli says:

    how that public lis works?
    and i have a question of how to remove object from dependent class and see the effect on other class?

Leave a Reply

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