Java Aggregation Tutorial with Real-Time Project Example
Last Updated on: 12th Nov 2025 10:56:46 AM
Welcome to this beginner-friendly tutorial on Aggregation in Java! Aggregation is a "HAS-A" relationship in Object-Oriented Programming (OOP). It represents a whole-part relationship where one object contains another object, but both can exist independently.
This tutorial uses a real-world project-based example — a Library Management System — to show how aggregation works in a practical application.
What is Aggregation?
Aggregation is a special form of association that represents a "HAS-A" relationship between two classes. The containing class has an instance of another class, but the contained object can exist independently.
-
Lifetime: The contained object is not destroyed when the container is destroyed.
-
Direction: One-way relationship (container → contained).
Example in Simple Words
-
A Department has Teachers.
-
If the Department is deleted, the Teachers still exist — this is Aggregation.
Aggregation Example in Java
// Address Class
class Address {
String city, state, country;
Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
// Employee Class - has an Address (Aggregation)
class Employee {
int id;
String name;
Address address;
Employee(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
void display() {
System.out.println(id + " " + name);
System.out.println(address.city + ", " + address.state + ", " + address.country);
}
}
// Main Class
public class AggregationExample {
public static void main(String[] args) {
Address addr = new Address("Pune", "Maharashtra", "India");
Employee emp = new Employee(101, "Sandip", addr);
emp.display();
}
}
Output
101 Sandip
Pune, Maharashtra, India
Explanation
-
The Employee class has a reference to the Address class.
-
This is a "has-a" relationship — Employee has an Address.
-
If the Employee object is destroyed, the Address object can still exist → Aggregation.
Real-Time Project: Library Management System
Let’s build a Library Management System where:
-
A Library HAS multiple Book objects.
-
A Book can exist even if the Library is closed or deleted.
-
A Book can be moved to another Library.
This is a perfect example of Aggregation.
Step 1: Define the Book Class
// Book.java
public class Book {
private String title;
private String author;
private String isbn;
private double price;
// Constructor
public Book(String title, String author, String isbn, double price) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.price = price;
}
// Getters
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getIsbn() { return isbn; }
public double getPrice() { return price; }
// Display book info
public void displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + isbn);
System.out.println("Price: ₹" + price);
System.out.println("------------------------");
}
}
Step 2: Define the Library Class (Container)
// Library.java
import java.util.ArrayList;
import java.util.List;
public class Library {
private String libraryName;
private String location;
private List<Book> books; // Aggregation: Library HAS-A Book
// Constructor
public Library(String libraryName, String location) {
this.libraryName = libraryName;
this.location = location;
this.books = new ArrayList<>(); // Initialize list
}
// Add a book to the library
public void addBook(Book book) {
books.add(book);
System.out.println("Book '" + book.getTitle() + "' added to " + libraryName);
}
// Remove a book
public void removeBook(Book book) {
if (books.remove(book)) {
System.out.println("Book '" + book.getTitle() + "' removed from " + libraryName);
} else {
System.out.println("Book not found in library.");
}
}
// Display all books
public void displayAllBooks() {
System.out.println("\n=== Books in " + libraryName + " (" + location + ") ===");
if (books.isEmpty()) {
System.out.println("No books available.");
} else {
for (Book book : books) {
book.displayInfo();
}
}
System.out.println("Total Books: " + books.size() + "\n");
}
// Get library info
public String getLibraryName() { return libraryName; }
}
Step 3: Main Application – LibraryManagementSystem
// LibraryManagementSystem.java
public class LibraryManagementSystem {
public static void main(String[] args) {
// Create independent Book objects
Book book1 = new Book("Java Programming", "James Gosling", "ISBN-001", 850.0);
Book book2 = new Book("Clean Code", "Robert Martin", "ISBN-002", 1200.0);
Book book3 = new Book("Design Patterns", "Erich Gamma", "ISBN-003", 1500.0);
// Create Libraries
Library centralLibrary = new Library("Central Public Library", "Mumbai");
Library branchLibrary = new Library("Andheri Branch", "Andheri, Mumbai");
// Add books to Central Library
centralLibrary.addBook(book1);
centralLibrary.addBook(book2);
// Add one book to Branch Library
branchLibrary.addBook(book2); // Same book in two libraries? Possible in real life!
// Display libraries
centralLibrary.displayAllBooks();
branchLibrary.displayAllBooks();
// Remove a book from Central Library
centralLibrary.removeBook(book1);
// Display again
centralLibrary.displayAllBooks();
// Prove Book exists independently
System.out.println("Book still exists outside library:");
book1.displayInfo();
// What if library is destroyed?
centralLibrary = null; // Library destroyed
System.out.println("Central Library destroyed, but book still exists:");
book1.displayInfo(); // Still works!
}
}
Output
Book 'Java Programming' added to Central Public Library
Book 'Clean Code' added to Central Public Library
Book 'Clean Code' added to Andheri Branch
=== Books in Central Public Library (Mumbai) ===
Title: Java Programming
Author: James Gosling
ISBN: ISBN-001
Price: ₹850.0
------------------------
Title: Clean Code
Author: Robert Martin
ISBN: ISBN-002
Price: ₹1200.0
------------------------
Total Books: 2
=== Books in Andheri Branch (Andheri, Mumbai) ===
Title: Clean Code
Author: Robert Martin
ISBN: ISBN-002
Price: ₹1200.0
------------------------
Total Books: 1
Book 'Java Programming' removed from Central Public Library
=== Books in Central Public Library (Mumbai) ===
Title: Clean Code
Author: Robert Martin
ISBN: ISBN-002
Price: ₹1200.0
------------------------
Total Books: 1
Book still exists outside library:
Title: Java Programming
Author: James Gosling
ISBN: ISBN-001
Price: ₹850.0
------------------------
Central Library destroyed, but book still exists:
Title: Java Programming
Author: James Gosling
ISBN: ISBN-001
Price: ₹850.0
------------------------
Key Observations (Why This is Aggregation)
|
Feature |
Proof in Example |
|---|---|
|
HAS-A Relationship |
Library has a List<Book> |
|
Independent Lifetime |
Book exists even after Library is set to null |
|
Reusable Object |
Same book2 used in two libraries |
|
No Ownership |
Deleting library doesn’t delete books |
Aggregation vs Composition
|
Aggregation |
Composition |
|---|---|
|
HAS-A (loose coupling) |
PART-OF (tight coupling) |
|
Contained object can exist independently |
Contained object destroyed with container |
|
Example: Library → Book |
Example: House → Room |
Composition Example:
class House {
private Room livingRoom = new Room(); // Room dies with House
}
Real-Time Use Cases of Aggregation
-
E-commerce: ShoppingCart HAS Product (products exist in database)
-
University: Department HAS Professor (professor can switch departments)
-
Hospital: Hospital HAS Doctor (doctor can work elsewhere)
-
Bank: Bank HAS Customer (customer can close account)
Summary
-
Aggregation = HAS-A relationship with independent lifetime.
-
Use when the contained object can exist without the container.
-
In real projects: Model reusable, independent entities.
-
Best Practice: Use List<ContainedClass> in the container class.
Project Tip:
In your Library Management System, use Aggregation between:
-
Library ↔ Book
-
Student ↔ BorrowedBook
-
Librarian ↔ ManagedSection
You now understand Aggregation in Java with a real-time, scalable project example!
Keep building — happy coding! ![]()