Aggregation in Java: A Must-Know OOP Concept for Interviews and Real-World Applications

Aggregation in Java is a type of association between two objects where one object contains a reference to another object, but the two objects have an independent life cycle. It is a "Has-A" relationship, meaning that one object "has" another object. However, unlike composition, in aggregation, the lifetime of the contained object does not depend on the container object. This means that even if the container object is destroyed, the contained object can still exist.

Key Points of Aggregation:

  1. "Has-A" relationship: Aggregation represents a "Has-A" relationship between two entities, such as "A car has an engine", but the car and the engine can exist independently.
  2. Loose coupling: Aggregation allows loose coupling between objects. The objects are related but can function independently.
  3. No ownership: Aggregation does not imply ownership. The object being referred to (the child) can exist independently of the parent object.
  4. Shared references: The aggregated object can be shared among multiple objects (parent classes) because of its independent lifecycle.

Aggregation Example in Java:

Let’s understand aggregation with an example. Consider the relationship between a Department and Employee. A Department can have multiple employees, but if a department is deleted, the employees still exist (they can belong to another department or be independent).

Code Example:

class Employee {

private String name;

public Employee(String name) {

this.name = name;

}


public String getName() {

return name;

}

}


class Department {

private String departmentName;

private Employee employee;


public Department(String departmentName, Employee employee) {

this.departmentName = departmentName;

this.employee = employee;

}


public void displayDetails() {

System.out.println("Department: " + departmentName);

System.out.println("Employee: " + employee.getName());

}

}


public class Main {

public static void main(String[] args) {

// Create an employee object

Employee emp1 = new Employee("John Doe");

// Create a department object that references the employee object

Department dept = new Department("Finance", emp1);

dept.displayDetails(); // Outputs department details along with                                             the employee

// Even if the department object is deleted or changed, the employee                     object still exists

}

}

Explanation:

  • Employee is a separate class that is used in the Department class.
  • The Department object holds a reference to an Employee object.
  • If the Department object is destroyed, the Employee object still exists.
  • This is a clear example of aggregation, where Employee can be shared across multiple departments (if we add more departments).

Difference Between Aggregation and Composition:

  • Aggregation: The child object can exist independently of the parent object. For example, an employee can exist without the department.
  • Composition: The child object is dependent on the parent object, and its lifecycle is tied to the parent. For example, a Car and its Engine (if we destroy the car, the engine also ceases to exist).

Composition Example:

class Engine {

public Engine() {

System.out.println("Engine created");

}

}


class Car {

private Engine engine; // A car "has-a" engine (composition)


public Car() {

engine = new Engine(); // Car creates its engine

}

}


public class Main {

public static void main(String[] args) {

Car myCar = new Car(); // Engine is created as part of the car

// The engine cannot exist independently without the car

}

}

In composition, if the Car object is destroyed, its Engine will also be destroyed because the Engine is part of the Car's lifecycle.

Key Differences Summarized:

AspectAggregationComposition
Life CycleIndependent life cycle.Dependent life cycle.
OwnershipNo ownership implied.Implies ownership.
ExampleEmployee and Department.Car and Engine.
RelationshipLoosely coupled, "Has-A".Strongly coupled, "Contains-A".

Real-Life Example of Aggregation:

  • Library and Books: A library "has" books, but the books are not dependent on the library. Books can exist without a library.
  • School and Students: A school can have students, but the students exist independently. If the school closes, students can still exist.

Aggregation is used to model real-world relationships where objects are related but can exist independently of each other.

#JavaAggregation #JavaProgramming #ObjectOrientedProgramming #JavaTutorials #HasArelationship
#JavaConcepts #JavaDevelopers #LearnJava #OOPinJava #JavaCode #JavaExamples #JavaBasics #JavaTips #JavaClassDesign

Post a Comment

Previous Post Next Post