Vijaya Kumar Chinthala
3 min readJul 16, 2024

IBDP — Option D- OOPS(JAVA)

Association, Composition and Aggregation in Java

Association: In Java, association is a relationship between two separate classes that are set up through their objects. It represents how objects from one class are connected to objects of another class. Association is a broader concept that includes specific relationships like aggregation and composition.

Association Example Code:

book class:

public class book {
private String title;
private author a;//Association Relationship
public book(String title,author a) {
this.title=title;
this.a=a;
}
public String gettitle() {
return title;
}
public author getauthor() {
return a;
}
}

author class:

public class author {

private String name;
public author(String name) {
this.name=name;
}
public String getname() {
return name;
}
}

main class:

public class main {

public static void main(String [] args) {
author a1=new author("author1");
book b1=new book("java1",a1);
author a2=new author("author2");
book b2=new book("java2",a2);
System.out.println("title,"+b1.gettitle()+",author,"+b1.getauthor().getname());
System.out.println("title,"+b2.gettitle()+",author,"+b2.getauthor().getname());
}
}

Aggregation: A specialized form of association where one class represents a whole and the other class represents a part. It is a “has-a” relationship with independent lifecycle. one entity will not effect other entity. It is a unidirectional association i.e. a one-way relationship.

Aggregation Example code:

lecturer class:

public class lecturer {

private String name;
public lecturer(String name) {
this.name=name;
}
public String getname() {
return name;
}
}

department class:

public class department {
private String dept;
private List<lecturer> lecturers;//Aggregation Relationship
public department(String dept) {
this.dept=dept;
this.lecturers=new ArrayList<>();
}
public String getdept() {
return dept;
}
public List<lecturer> getlecturer() {
return lecturers;
}
public void addlecturer(lecturer l) {
lecturers.add(l);
}

}

main class:

public class main {

public static void main(String [] args) {
lecturer l1=new lecturer("lectuerer1");
lecturer l2=new lecturer("lectuere2");
lecturer l3=new lecturer("lectuere3");
department cse=new department("CSE");
cse.addlecturer(l1);
cse.addlecturer(l2);
cse.addlecturer(l3);
System.out.println("Dept:"+cse.getdept());
for (lecturer lecturers:cse.getlecturer()) {
System.out.println("lectuers:"+lecturers.getname());
}
}
}

output:

Dept:CSE
lectuers:lectuerer1
lectuers:lectuere2
lectuers:lectuere3

Composition: A stronger form of aggregation with a dependent lifecycle. If the parent object is destroyed, the child objects will also be destroyed. It represents part-of relationship. Here both entities are dependent.

Composition Sample Code:

room class:

public class room {
private String name;
public room(String name) {
this.name=name;
}
public String getname() {
return name;
}

}

house class:

public class house {
private List<room> rooms; //Composition Relationship
public house() {
this.rooms=new ArrayList<>();
rooms.add(new room("Living Room"));
rooms.add(new room("Hall"));
}
public List<room> getrooms(){
return rooms;
}

}

main class

public class main {

public static void main(String [] args) {
house h1=new house();
System.out.println("House\n");
for (room rooms:h1.getrooms()) {
System.out.println(rooms.getname());
}
}
}

Happy coding

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response