You are on page 1of 16

Association, Aggregation, and

Composition in Java

Association
Association is a relationship between two separate classes
that establishes through their objects. Each objects have their
own life-cycle and there is no owner. Association can be one-
to-one, one-to-many, many-to-one, many-to-many.

Let's take an example of Teacher and Student. Multiple


students can associate with single teacher and single student
can associate with multiple teachers, but there is no
ownership between the objects and both have their own life-
Association, Aggregati
cycle. Both can be created and deleted independently.
Teacher.java

1 import java.util.ArrayList;
3
4 public class Teacher {
5
6 private final String name;
7 private final ArrayList<Student> students = new ArrayList<>();
8
9 // teacher name
10 Teacher(String name) {
11 this.name = name;
12 }
13
14 public String getName() {
15 return this.name;
16 }
17
18 public void addStudent(Student student) {
19 student.addTeacher(this);
20 this.students.add(student);
21 }
22
23 public ArrayList<Student> getStudents() {
24 return students;
25 }
26
27 public void print() {
28 System.out.println("Teacher " + this.name + "'s students are:");
29 for (Student student:this.students) {
30 System.out.println("- " + student.getName());
31 }
32 }
33 }

Student.java

1 import java.util.ArrayList;
3
4 public class Student {
5
6 private final String name;
7 private final ArrayList<Teacher> teachers = new ArrayList<>();
8
9 // student name
10 Student(String name) {
11 this.name = name;
12 }
13
14 public String getName() {
15 return this.name;
16 }
17
18 public void addTeacher(Teacher teacher) {
19 this.teachers.add(teacher);
20 }
21
22 public ArrayList<Teacher> getTeachers() {
23 return teachers;
24 }
25
26 public void print() {
27 System.out.println("Student " + this.name + "'s teachers are:");
28 for (Teacher teacher:this.teachers) {
29 System.out.println("- " + teacher.getName());
30 }
31 }
32 }
Association.java

1 public class Association {


2
3 public static void main(String[] args) {
4 Teacher teacher1 = new Teacher("Onizuka");
5 Teacher teacher2 = new Teacher("Fuyutsuki");
6
7 Student student1 = new Student("Nomura");
8 Student student2 = new Student("Aizawa");
9 Student student3 = new Student("Yoshikawa");
10 Student student4 = new Student("Uehara");
11
12 teacher1.addStudent(student1);
13 teacher1.addStudent(student2);
14 teacher1.addStudent(student3);
15
16 teacher2.addStudent(student2);
17 teacher2.addStudent(student3);
18 teacher2.addStudent(student4);
19
20 teacher1.print();
21 teacher2.print();
22 student1.print();
23 student2.print();
24 student3.print();
25 student4.print();
26 }
27 }
28
29 /*
30 Output:
31 ------
32 Teacher Onizuka's students are:
33 - Nomura
34 - Aizawa
35 - Yoshikawa
36 Teacher Fuyutsuki's students are:
37 - Aizawa
38 - Yoshikawa
39 - Uehara
40 Student Nomura's teachers are:
41 - Onizuka
42 Student Aizawa's teachers are:
43 - Onizuka
44 - Fuyutsuki
45 Student Yoshikawa's teachers are:
46 - Onizuka
47 - Fuyutsuki
48 Student Uehara's teachers are:
49 - Fuyutsuki
50 */

Aggregation and Composition are subsets of


association meaning they are specific cases of
association.

Aggregation
Aggregation is a specialized form of Association where
all objects have their own life cycle, where the child can
exist independently of the parent. Aggregation is also
called a “Has-a” relationship.

Let's take an example of Supervisor and Subordinate. An


employee (as a subordinate) can not belong to multiple
supervisors, but if we delete the supervisor, the Associatio
employee object (subordinate) will not be destroyed. We can think about it as a “has-a”
relationship.

Employee.java

1 import java.util.ArrayList;
3
4 public class Employee {
5
6 private final String name;
7 private Employee supervisor;
8 private final ArrayList<Employee> subordinates = new ArrayList<>();
9
10 // teacher name
11 Employee(String name) {
12 this.name = name;
13 }
14
15 public String getName() {
16 return this.name;
17 }
18
19 public Employee getSupervisor() {
20 return supervisor;
21 }
22
23
24 public void setSupervisor(Employee supervisor) {
25 this.supervisor = supervisor;
26 supervisor.subordinates.add(this);
27 }
28
29 public void print() {
30 System.out.println("Employee " + this.name + "'s supervisor is:" +
31 (this.supervisor==null?"N.A.":supervisor.getName()));
32 System.out.println("Employee " + this.name + "'s subordinates are:");
33 for (Employee employee:this.subordinates) {
34 System.out.println("- " + employee.getName());
35 }
36 }
37 }

Aggregation.java

1 public class Aggregation {


2
3 public static void main(String[] args) {
4 Employee employee1 = new Employee("Systrom");
5 Employee employee2 = new Employee("Krieger");
6 Employee employee3 = new Employee("Riedel");
7 Employee employee4 = new Employee("Sweeney");
8 Employee employee5 = new Employee("Zollman");
9 Employee employee6 = new Employee("Cole");
10 Employee employee7 = new Employee("Hochmuth");
11 Employee employee8 = new Employee("McAllister");
12
13 employee3.setSupervisor(employee1);
14 employee4.setSupervisor(employee1);
15 employee5.setSupervisor(employee1);
16 employee6.setSupervisor(employee2);
17 employee7.setSupervisor(employee2);
18 employee8.setSupervisor(employee2);
19
20 employee1.print();
21 employee2.print();
22 employee3.print();
23 employee8.print();
24 }
25 }
26
27 /*
28 Output:
29 ------
30 Employee Systrom's supervisor is:N.A.
31 Employee Systrom's subordinates are:
32 - Riedel
33 - Sweeney
34 - Zollman
35 Employee Krieger's supervisor is:N.A.
36 Employee Krieger's subordinates are:
37 - Cole
38 - Hochmuth
39 - McAllister
40 Employee Riedel's supervisor is:Systrom
41 Employee Riedel's subordinates are:
42 Employee McAllister's supervisor is:Krieger
43 Employee McAllister's subordinates are:
44 */

In above example, we use Employee class as type of Supervisor and Subordinate. The
relationship is established between objects (class instances). Feel free to create
specialized class Supervisor for supervisor.
Composition
Composition is specialized form of Aggregation and we can call this as a “death”
relationship. Child object does not have its life-cycle and if parent object is deleted, all
child objects will also be deleted.

Let's take an example of Car and Engine. Car is the owner of engine, engine is created
when Car is created, and it's destroyed when Car is destroyed.

Engine.java

1 public class Engine {


2
3 private final String engineType;
4
5 public String getEngineType() {
6 return engineType;
7 }
8
9 public Engine(String engineType) {
10 this.engineType = engineType;
11 }
12 }

Car.java

1 public class Car {


2
3 private final String name;
4 private final Engine engine;
5
6 public String getName() {
7 return name;
8 }
9
10 public Engine getEngine() {
11 return engine;
12 }
13
14 public Car(String name, Engine engine) {
15 this.name = name;
16 this.engine = engine;
17 }
18
19 public void print() {
20 System.out.println("Car " + this.name +
21 " engine is " + this.engine.getEngineType().toString());
22 }
23 }

Composition.java

1 public class Composition {


2
3 public static void main(String[] args) {
4 Car car1 = new Car("Peugeot 3008", new Engine(EngineType.PETROL));
5 Car car2 = new Car("BMW X5 Diesel", new Engine(EngineType.DIESEL));
6
7 car1.print();
8 car2.print();
9 }
10 }
11
12 /*
13 Output:
14 ------
15 Car Peugeot 3008 engine is PETROL
16 Car BMW X5 Diesel engine is DIESEL
17 */

Summary
Let's check below table for association, aggregation and composition brief summary:

Association Aggregation Composition

Special type of Association. Special type of Aggregation


Related to
Association
Weak association Strong association

Relation Has-A Owns

one object is the owner of another one object is contained in another


object. object

Owner No owner Single owner Single owner

Life-cycle own life- own life-cycle owner life-cycle


cycle

Child object independent belong to single parent belong to single parent

n-gl.com

You might also like