Understanding Association, Aggregation, and Composition in Object-Oriented Programming

Anh Trần Tuấn - Aug 30 - - Dev Community

1. What is Association?

Association represents a relationship where objects of one class are linked to objects of another class. This relationship can be one-to-one, one-to-many, or many-to-many. Association is the most general form of relationship and doesn’t imply ownership or lifecycle dependency between the objects.

1.1 One-to-One Association

In a one-to-one association, each object of a class is associated with exactly one object of another class. For example, a Person class and a Passport class where each person has exactly one passport.

Image

public class Person {
    private Passport passport;

    public Person(Passport passport) {
        this.passport = passport;
    }

    public Passport getPassport() {
        return passport;
    }
}

public class Passport {
    private String number;

    public Passport(String number) {
        this.number = number;
    }

    public String getNumber() {
        return number;
    }
}
Enter fullscreen mode Exit fullscreen mode

Demo:

public class Main {
    public static void main(String[] args) {
        Passport passport = new Passport("123456789");
        Person person = new Person(passport);

        System.out.println("Passport Number: " + person.getPassport().getNumber());
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Passport Number: 123456789
Enter fullscreen mode Exit fullscreen mode

1.2 One-to-Many Association

In a one-to-many association, an object of one class is associated with multiple objects of another class. For example, a Teacher class and a Student class where one teacher can have multiple students.

Image

Example:

import java.util.ArrayList;
import java.util.List;

public class Teacher {
    private List<Student> students = new ArrayList<>();

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

    public List<Student> getStudents() {
        return students;
    }
}

public class Student {
    private String name;

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

    public String getName() {
        return name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Demo:

public class Main {
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.addStudent(new Student("John"));
        teacher.addStudent(new Student("Jane"));

        for (Student student : teacher.getStudents()) {
            System.out.println("Student Name: " + student.getName());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Student Name: John
Student Name: Jane
Enter fullscreen mode Exit fullscreen mode

2. What is Aggregation?

Aggregation is a special form of association with a whole-part relationship where the child can exist independently of the parent. This signifies a weaker relationship than composition, meaning the lifecycle of the child object is not dependent on the parent.

2.1 Example of Aggregation

Consider a Library and Book classes. A library can have multiple books, but books can exist without the library.

Example:

import java.util.ArrayList;
import java.util.List;

public class Library {
    private List<Book> books = new ArrayList<>();

    public void addBook(Book book) {
        books.add(book);
    }

    public List<Book> getBooks() {
        return books;
    }
}

public class Book {
    private String title;

    public Book(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}
Enter fullscreen mode Exit fullscreen mode

Demo:

public class Main {
    public static void main(String[] args) {
        Library library = new Library();
        library.addBook(new Book("The Catcher in the Rye"));
        library.addBook(new Book("To Kill a Mockingbird"));

        for (Book book : library.getBooks()) {
            System.out.println("Book Title: " + book.getTitle());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Book Title: The Catcher in the Rye
Book Title: To Kill a Mockingbird
Enter fullscreen mode Exit fullscreen mode

3. What is Composition?

Composition is a form of association where the child’s lifecycle is dependent on the parent’s lifecycle. If the parent object is destroyed, the child objects are also destroyed. This implies a strong relationship and ownership.

3.1 Example of Composition

Consider a House and Room classes where a house contains rooms. Rooms cannot exist without a house.

Example:

import java.util.ArrayList;
import java.util.List;

public class House {
    private List<Room> rooms = new ArrayList<>();

    public House() {
        rooms.add(new Room("Living Room"));
        rooms.add(new Room("Bedroom"));
    }

    public List<Room> getRooms() {
        return rooms;
    }

    private class Room {
        private String name;

        public Room(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Demo:

public class Main {
    public static void main(String[] args) {
        House house = new House();

        for (House.Room room : house.getRooms()) {
            System.out.println("Room Name: " + room.getName());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Room Name: Living Room
Room Name: Bedroom
Enter fullscreen mode Exit fullscreen mode

4. Conclusion

In summary, understanding the differences between Association, Aggregation, and Composition is essential for designing effective object-oriented systems. Each relationship type reflects different levels of dependency and ownership, helping you model real-world scenarios accurately.

If you have any questions or need further clarification, feel free to comment below!

Read posts more at : Understanding Association, Aggregation, and Composition in Object-Oriented Programming

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player