AssociationAggregationCompositionJava No
AssociationAggregationCompositionJava No
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.
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
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.
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
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
Car.java
Composition.java
Summary
Let's check below table for association, aggregation and composition brief summary:
n-gl.com