Associations(Composition and Aggregation) or has-A Relationship in Java.

Vipul Gupta
2 min readMay 12, 2020

Has-A Relationship is also known as Composition and Aggregation in java.

Composition: Without an existing container object if there is no chance of the existing contained object, then container and contained objects are strongly associated, and this strong association is known as Composition.

Example: University consists of several departments, without existing University there is no chance of existing department, hence University and Department are strongly associated.

Aggregation: Without an existing container object if there is a chance of the existing contained object, then container and contained objects are weakly associated, and this weak association is known as Aggregation.

Example: Department consists of several Professors, without the existing Department there may be a chance of existing Professors Objects, hence Department and Professors are weakly associated.

Below is the implementation of both the concepts discussed above.

OOPS Concepts in Java
/*This is the code for Association topic from Java or has-a relationship
* if University doesn't exist then Department will also not exist --> Strong Association or Composition
* but if University doesn't exist then also the Professors will exist--> Weak Association or Aggregation*/
package com.practice.oops;import java.util.ArrayList;
import java.util.List;
class Department {
private String deptName;
private String deptCode;
private String deptBudget;
Department(String deptName, String deptCode, String deptBudget) {
this.deptName = deptName;
this.deptCode = deptCode;
this.deptBudget = deptBudget;
}
public String toString() {
return this.deptName + " " + this.deptCode + " " + this.deptBudget;
}
}class Professors{
private String profName;

public Professors(String profName) {
this.profName=profName;
}

public String toString() {
return profName;
}
}
class University {
private String clgName;

private List<Department> listOfDept;
private List<Professors> listofProf;
public University(String clgName,List<Professors> listOfProf) {
this.clgName = clgName;
this.listofProf=listOfProf;
Department CSE = new Department("CSE", "CSE_01", "200k");
Department ECE = new Department("EC", "EC_01", "300k");
Department MECH = new Department("MECHANICAL", "ME_01", "500k");
Department CE = new Department("CIVIL", "CE_01", "600k");
this.listOfDept = new ArrayList<Department>();
listOfDept.add(CSE);
listOfDept.add(ECE);
listOfDept.add(MECH);
listOfDept.add(CE);
}
public String toString() {
return "University Name is " + this.clgName + " || "
+ "List of Department are :" + this.listOfDept+"|| "+"List of Professors :"+this.listofProf;
}
}public class AggregationAndComposition {public static void main(String[] args) {
ArrayList<Professors> listOfProf=new ArrayList<Professors>();
Professors prof1=new Professors("Professor_01");
Professors prof2=new Professors("Professor_02");
Professors prof3=new Professors("Professor_03");
Professors prof4=new Professors("Professor_04");
Professors prof5=new Professors("Professor_05");
listOfProf.add(prof1);
listOfProf.add(prof2);
listOfProf.add(prof3);
listOfProf.add(prof4);
listOfProf.add(prof5);


University university = new University("ABC Institute Of Technology",listOfProf);

//If you uncomment the below line then you see only list of Professors still exist, all the departments are gone
//university = null;
System.out.println(university);
for(Professors prof:listOfProf)
System.out.println(prof);
}
}

Thank you for reading.

--

--