Unit 4 · Association, Aggregation and Composition

4.1Association

Objects rarely work alone. A program is a group of objects that know about each other and work together. Unit 4 is about the different strengths of these links, starting with the loosest: association.

Association is a general relationship where one object uses or knows about another. Both objects have independent lifetimes; neither owns the other.

Examples: a Teacher teaches Students; a Doctor treats Patients; a Customer places Orders.

Multiplicity (cardinality): one-to-one (1:1), one-to-many (1:N), many-to-many (M:N).

Java
class Teacher {
    private String name;
    Teacher(String name) { this.name = name; }
    String getName() { return name; }
}

class Student {
    private String name;
    Student(String name) { this.name = name; }
    String getName() { return name; }

    // association: a student is taught by a teacher
    void attend(Teacher t) {
        System.out.println(name + " attends the class of " + t.getName());
    }
}

Teacher t = new Teacher("Ms. Perera");
Student s = new Student("Nimal");
s.attend(t);
StudentTeacher1..*1attends class ofdelete either object and the other still exists
Association: a Student uses a Teacher; both objects live independently

If the student object disappears, the teacher object continues to exist, and the other way round.

Association in code: keeping a reference

The example above passes the Teacher into one method call. Strictly, UML calls such a short, temporary link a dependency (Student uses a Teacher for a moment). A full association is usually shown in code by one object keeping a reference to the other in an attribute, so that the link lasts:

Java
class Student {
    private String name;
    private Teacher classTeacher;        // association: a lasting link

    Student(String name) { this.name = name; }

    void setClassTeacher(Teacher t) { this.classTeacher = t; }

    void showTeacher() {
        System.out.println(name + "'s class teacher is " + classTeacher.getName());
    }
}

The Teacher is still created outside the Student and can be shared by many students. Setting a student's teacher does not create or destroy anybody.

Direction

Use one-way links unless the program really needs to navigate both ways.

Multiplicity in code

MultiplicityMeaningTypical Java
1 to 1A Person has one Passportprivate Passport passport;
1 to manyA Teacher teaches many Studentsprivate List<Student> students = new ArrayList<>();
Many to manyStudents take many Courses; Courses have many StudentsA list on each side, or a separate Enrolment class

For many-to-many links, a separate class is often the cleanest design, because the link itself usually has data: an Enrolment can record the date and the grade for one student on one course.

Remember

  • Association: objects know about or use each other, but neither owns the other.
  • Both objects are created separately and have independent lifetimes.
  • Multiplicity says how many objects are on each end: 1:1, 1:N or M:N.