Composition Vs Aggregation in Java

Extending your code

 
Extensibility of a class doesn't always depend on inheritance. Using composition and aggregation relationships between classes, we can create more cohesive and loosely coupled modules. Did I say no inheritance?
 
inheritance
 
Let's first understand composition and aggregation in the real world. Let's say you are a student pursuing studies in a college in the Computer Science department. If you are wearing the Object-Oriented Analysis and Design (OOAD) shoes you could easily understand the entities from the scenario. You guessed it! Student, College and Department.
 
A college has many departments and many students. The departments of the college can't exist without the college itself . This means “Departments” are owned by the college. Students can exist without the college, the ownership of students are not in the college alone. Students can look for a new college if the college doesn't exist. Can you identify a composition and aggregation relationship?
 
Composition is the idea of an object could be made up of, or composed of, other objects. The composed objects are intrinsic to the main object. Aggregation states that an object can bring together separate objects that has their own lifetime. The relation between body and heart is composition whereas car and wheel is aggregation.
 
data flow diagram
 
Let's look at a more practical example in Java. An employee has an address and insurance. The insurance is optional for the employee but the employee should always have an address.
 
employee has an address and insurance
 
The lifetime of the address instance is intrinsic to the employee instance, but an insurance instance can exist on its own. If you see the code below in the Test class, once the employee instance is created, the address instance is also created within the employee class. The insurance instance is set explicitly and it is optional for an employee.
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         Employee kannan = new Employee("kannan sudhakaran""Andheri""mumbai""Maharashtra""5055");  
  6.         System.out.println(kannan);  
  7.   
  8.   
  9.         //add via aggregation  
  10.         InsuranceInfo insuranceInfo = new InsuranceInfo();  
  11.         insuranceInfo.setPolicyId("1001");  
  12.         insuranceInfo.setPolicyName("Life Insurance policy");  
  13.   
  14.         kannan.setInsurance(insuranceInfo);  
  15.   
  16.         System.out.println(kannan);  
  17.     }  
  18.   
  19. }  
address
 
A quick sketch of the class diagram should look like this.
 
class diagram
 
The following is the implementation in Java.
  1. // Employee.Java  
  2. package com.techlabs.composition.aggregation;  
  3. public class Employee {  
  4.     private String name;  
  5.     private AddressInfo address;  
  6.     private InsuranceInfo insurance; // may or may not  
  7.   
  8.   
  9.     public Employee(String name, String street, String city, String state, String postalCode) {  
  10.         this.name = name;  
  11.         this.address = new AddressInfo();  
  12.   
  13.         address.setCity(city);  
  14.         address.setState(state);  
  15.         address.setPostalCode(postalCode);  
  16.         address.setStreet(street);  
  17.   
  18.   
  19.   
  20.     }  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.     public AddressInfo getAddress() {  
  28.         return address;  
  29.     }  
  30.     public void setAddress(AddressInfo address) {  
  31.         this.address = address;  
  32.     }  
  33.     public InsuranceInfo getInsurance() {  
  34.         return insurance;  
  35.     }  
  36.     public void setInsurance(InsuranceInfo insurance) {  
  37.         this.insurance = insurance;  
  38.     }  
  39.   
  40.     @Override  
  41.     public String toString() {  
  42.   
  43.         StringBuilder retValue = new StringBuilder();  
  44.         retValue.append(name).append(" ")  
  45.             .append(address.getStreet()).append(" ")  
  46.             .append(address.getCity()).append(" ")  
  47.             .append(address.getState()).append(" ");  
  48.   
  49.         if (insurance != null) retValue.append(insurance.getPolicyId()).append(" ")  
  50.             .append(insurance.getPolicyName());  
  51.   
  52.         return retValue.toString();  
  53.   
  54.   
  55.     }  
  56.   
  57. }  
  58. // AddressInfo.Java  
  59. package com.techlabs.composition.aggregation;  
  60. public class AddressInfo {  
  61.     private String street;  
  62.     private String city;  
  63.     private String state;  
  64.     private String postalCode;  
  65.   
  66.     public String getStreet() {  
  67.         return street;  
  68.     }  
  69.     public void setStreet(String street) {  
  70.         this.street = street;  
  71.     }  
  72.     public String getCity() {  
  73.         return city;  
  74.     }  
  75.     public void setCity(String city) {  
  76.         this.city = city;  
  77.     }  
  78.     public String getState() {  
  79.         return state;  
  80.     }  
  81.     public void setState(String state) {  
  82.         this.state = state;  
  83.     }  
  84.     public String getPostalCode() {  
  85.         return postalCode;  
  86.     }  
  87.     public void setPostalCode(String postalCode) {  
  88.         this.postalCode = postalCode;  
  89.     }  
  90. }  
  91. //InsuranceInfo.Java  
  92. package com.techlabs.composition.aggregation;  
  93. public class InsuranceInfo {  
  94.     private String policyName;  
  95.     private String policyId;  
  96.   
  97.     public String getPolicyName() {  
  98.         return policyName;  
  99.     }  
  100.     public void setPolicyName(String policyName) {  
  101.         this.policyName = policyName;  
  102.     }  
  103.     public String getPolicyId() {  
  104.         return policyId;  
  105.     }  
  106.     public void setPolicyId(String policyId) {  
  107.         this.policyId = policyId;  
  108.     }  
  109. }  


Similar Articles