Composition
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
- It represents part-of relationship.
- In composition, both entities are dependent on each other.
- When there is a composition between two entities, the composed object cannot exist without the other entity.
Composition Example:
// Java program to illustrate
// Concept of Composition
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Department class
class Department {
// Attributes of Department
public String departmentName;
// Constructor of Department class
public Department(String departmentName)
{
this.departmentName = departmentName;
}
public String getDepartmentName()
{
return departmentName;
}
}
// Class 2
// Company class
class Company {
// Reference to refer to list of books
private String companyName;
private List<Department> departments;
// Constructor of Company class
public Company(String companyName)
{
this.companyName = companyName;
this.departments = new ArrayList<Department>();
}
// Method
// to add new Department to the Company
public void addDepartment(Department department)
{
departments.add(department);
}
public List<Department> getDepartments()
{
return new ArrayList<>(departments);
}
// Method
// to get total number of Departments in the Company
public int getTotalDepartments()
{
return departments.size();
}
}
// Class 3
// Main class
class CompositonExample {
// Main driver method
public static void main(String[] args)
{
// Creating a Company object
Company techCompany = new Company("Tech Corp");
techCompany.addDepartment(new Department("Engineering"));
techCompany.addDepartment(new Department("Operations"));
techCompany.addDepartment(new Department("Human Resources"));
techCompany.addDepartment(new Department("Finance"));
int d = techCompany.getTotalDepartments();
System.out.println("Total Departments: " + d);
System.out.println("Department names: ");
for (Department dept : techCompany.getDepartments()) {
System.out.println("- " + dept.getDepartmentName());
}
}
}
Output
Total Departments: 4 Department names: - Engineering - Operations - Human Resources - Finance
Explanation of the above Program:
In the above example,
- A company can have no. of departments.
- All the departments are part-of the Company.
- So, if the Company gets destroyed then all the Departments within that particular Company will be destroyed, i.e. Departments can not exist independently without the Company.
- That’s why it is composition. Department is Part-of Company.
Association, Composition and Aggregation in Java
Association is a relation between two separate classes which is established through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. In Object-Oriented programming, an Object communicates to another object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.