Class Relationships

Association

A relationship between two classes where objects of one class use or interact with objects of another class. Both objects exist independently.

8 min readEssential

1What is Association?

Real-World Analogy
Teacher and Student

A teacher teaches students. Both exist independently - a teacher can exist without students, and students can exist without a specific teacher. They are associated but not dependent.

Doctor and Patient

A doctor treats patients. Neither owns the other. A patient can visit multiple doctors, and a doctor can treat multiple patients. This is a loose relationship.

Association is a "uses-a" or "knows-about" relationship between classes. Objects of associated classes have independent lifecycles. Neither class owns the other - they simply interact or reference each other.

UML Notation: Types of Relationships

Teacher
Student
Simple line with arrow = Association (uses/knows about)
Team
Player
Empty diamond = Aggregation (weak HAS-A)
Car
Engine
Filled diamond = Composition (strong HAS-A)

2Multiplicity in Association

Multiplicity defines how many objects can participate in the relationship.

One-to-One (1:1)
Person - Passport
One person has exactly one passport
1 ——— 1
One-to-Many (1:*)
Department - Employee
One department has many employees
1 ——— *
Many-to-One (*:1)
Student - School
Many students belong to one school
* ——— 1
Many-to-Many (*:*)
Student - Course
Students take many courses, courses have many students
* ——— *
Common Multiplicity Notations
1 = exactly one
0..1 = zero or one
* = zero or more
1..* = one or more

3Implementation

Association Example - Teacher and Students
// ========== Association: Teacher teaches Students ==========

class Student {
    private String name;
    private String id;

    public Student(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public String getName() { return name; }
    public String getId() { return id; }

    public void learn(String subject) {
        System.out.println(name + " is learning " + subject);
    }
}

class Teacher {
    private String name;
    private String subject;
    private List<Student> students;  // Association with Students

    public Teacher(String name, String subject) {
        this.name = name;
        this.subject = subject;
        this.students = new ArrayList<>();
    }

    // Association methods
    public void addStudent(Student student) {
        students.add(student);
    }

    public void removeStudent(Student student) {
        students.remove(student);
    }

    public void teach() {
        System.out.println(name + " is teaching " + subject);
        for (Student student : students) {
            student.learn(subject);
        }
    }
}

// Usage
public class Main {
    public static void main(String[] args) {
        // Create objects independently
        Student alice = new Student("Alice", "S001");
        Student bob = new Student("Bob", "S002");
        Teacher john = new Teacher("John", "Mathematics");

        // Associate them
        john.addStudent(alice);
        john.addStudent(bob);
        john.teach();

        // Objects exist independently
        john.removeStudent(alice);
        // alice still exists - not destroyed
        System.out.println(alice.getName() + " still exists!");

        // Teacher can be destroyed, students survive
        john = null;
        System.out.println(bob.getName() + " is still alive!");
    }
}
Output:
John is teaching Mathematics
Alice is learning Mathematics
Bob is learning Mathematics
Alice still exists!
Bob is still alive!

4Unidirectional vs Bidirectional

Unidirectional
One class knows about the other, but not vice versa.
TeacherStudent
Teacher knows students, Student doesn't know teacher
Bidirectional
Both classes know about each other.
TeacherStudent
Teacher has List of Students, Student has Teacher ref
Prefer Unidirectional
Bidirectional associations are harder to maintain and can lead to circular dependencies. Prefer unidirectional when possible. Add the reverse direction only when truly needed.

5Interview Follow-up Questions

Interview Follow-up Questions

Common follow-up questions interviewers ask

6Key Takeaways

1Association = uses-a or knows-about relationship.
2Objects have independent lifecycles.
3Can be unidirectional (A→B) or bidirectional (A↔B).
4Multiplicity: 1:1, 1:*, *:1, or *:*.
5Prefer unidirectional to avoid circular dependencies.
6Weaker than aggregation and composition.