Introduction
Relationships between classes in Java describe how objects of different classes connect, interact, or depend on one another.
The three common class relationships are:
Association
Aggregation
Composition
Association is a general connection between classes, while aggregation and composition represent different strengths of a whole-part or has-a relationship. In UML, aggregation and composition are represented as specialized relationships between objects.
1. Association in Java
Association is a relationship in which objects of two independent classes interact with each other.
Neither object owns the other, and both can exist independently.
For example, a doctor treats a patient. The doctor and patient can exist separately even when they are not connected.
Association Example
// Patient is an independent classclass Patient { // Store the patient's name String name; // Initialize the patient's name Patient(String name) { this.name = name; }}// Doctor is another independent classclass Doctor { // Store the doctor's name String name; // Initialize the doctor's name Doctor(String name) { this.name = name; } // Doctor uses a Patient object temporarily void treat(Patient patient) { System.out.println(name + " treats " + patient.name + "."); }}public class Main { public static void main(String[] args) { // Create both objects independently Doctor doctor = new Doctor("Dr. Meera"); Patient patient = new Patient("Rahul"); // Create an association through method interaction doctor.treat(patient); // Output :- Dr. Meera treats Rahul. }}Here, the Doctor object uses the Patient object, but neither object owns the other.
Association
2. Aggregation in Java
Aggregation is a weak has-a relationship in which one class contains a reference to an object of another class.
The contained object is created independently and can continue to exist without the container object.
For example, a department has teachers, but a teacher can exist without that particular department.
Aggregation Example
// Teacher can exist independentlyclass Teacher { // Store the teacher's name String name; // Initialize the teacher's name Teacher(String name) { this.name = name; }}// Department contains a reference to Teacherclass Department { // Reference to an independently created Teacher Teacher teacher; // Receive the Teacher object from outside Department(Teacher teacher) { this.teacher = teacher; } // Display the aggregated object's information void showTeacher() { System.out.println("Teacher: " + teacher.name); }}public class Main { public static void main(String[] args) { // Create the Teacher independently Teacher teacher = new Teacher("Anita"); // Pass the existing Teacher to the Department Department department = new Department(teacher); // Access the Teacher through the Department department.showTeacher(); // Output :- Teacher: Anita }}The Department contains a Teacher, but it does not create that teacher. The Teacher object can still exist independently.
Aggregation
3. Composition in Java
Composition is a strong has-a relationship in which one class owns an object of another class.
The contained object is created and managed by the owner and is not intended to exist independently from it.
For example, a house contains rooms that belong to that house.
Composition Example
// Room represents a part of a Houseclass Room { // Store the type of room String type; // Initialize the room type Room(String type) { this.type = type; }}// House strongly owns its Roomclass House { // Room is stored as part of the House private Room room; // House creates and manages its own Room House() { room = new Room("Bedroom"); } // Display information about the owned Room void showRoom() { System.out.println("House contains: " + room.type); }}public class Main { public static void main(String[] args) { // Creating a House also creates its Room House house = new House(); // Access the Room through the House house.showRoom(); // Output :- House contains: Bedroom }}In this example, the House creates and owns the Room. The room is treated as a part of that specific house.
Composition
Association vs Aggregation vs Composition
Relationship | Meaning | Ownership | Object Life Cycle | Example |
|---|---|---|---|---|
Association | One object interacts with another | No ownership | Both objects are independent | Doctor and Patient |
Aggregation | Weak has-a relationship | Weak ownership | The part can exist independently | Department and Teacher |
Composition | Strong has-a relationship | Strong ownership | The part depends logically on the whole | House and Room |
Summary
Association, aggregation, and composition describe how Java classes interact.
Association connects independent objects. Aggregation creates a weak has-a relationship in which the contained object can exist independently. Composition creates a stronger relationship in which the contained object is owned and managed by the containing object.
Be the first to add a comment.