Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .

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

5.00/5 (1 vote)
29 Aug 2024CPOL2 min read 1.4K  
In object-oriented programming (OOP), understanding the relationships between classes is crucial for designing robust and maintainable systems. The three fundamental types of relationships are Association, Aggregation, and Composition.

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;
    }
}
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());
    }
}
Result:
Passport Number: 123456789

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;
    }
}
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());
        }
    }
}
Result:
Student Name: John
Student Name: Jane

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;
    }
}
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());
        }
    }
}
Result:
Book Title: The Catcher in the Rye
Book Title: To Kill a Mockingbird

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;
        }
    }
}
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());
        }
    }
}
Result:
Room Name: Living Room
Room Name: Bedroom

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)