What is difference between aggregation composition and association in java
Sure, let’s summarize the differences between association, aggregation, and composition in Java with brief examples for each.
Association:
Definition: Association represents a relationship between two or more classes where objects of one class are related to objects of another class. It can be one-to-one, one-to-many, or many-to-many.
Example: Consider two classes Teacher
and Student
. They are associated because a teacher can teach multiple students.
/*
* Author: Zameer Ali
* */
class Teacher {
// Teacher properties and methods
}
class Student {
// Student properties and methods
}
// Association between Teacher and Student classes
class School {
private Teacher teacher;
private List<Student> students;
// School properties and methods
}
Aggregation:
Definition: Aggregation is a specialized form of association where one class contains objects of another class, but the contained objects can exist independently.
Example: Consider classes Department
and Employee
. A department can have multiple employees. If the department is closed, employees can still exist.
/*
* Author: Zameer Ali
* */
class Department {
// Department properties and methods
}
class Employee {
// Employee properties and methods
}
// Aggregation between Department and Employee classes
class Company {
private List<Employee> employees;
// Company properties and methods
}
Composition:
Definition: Composition is a strong form of association where one class contains objects of another class, and the contained objects cannot exist without the container class.
Example: Consider classes Car
and Engine
. A car is composed of an engine. If the car is scrapped, the engine becomes useless.
/*
* Author: Zameer Ali
* */
class Engine {
// Engine properties and methods
}
class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
// Car properties and methods
}
Certainly, let’s summarize the key differences between aggregation, composition, and association in Java:
Association:
- Definition: Association represents a relationship between two or more classes where objects of one class are related to objects of another class.
- Dependency Level: It represents a weaker relationship where objects are related, but one class doesn’t own the other. Objects can exist independently.
- Example: Consider classes
Teacher
andStudent
. They are associated because a teacher can teach multiple students. Neither owns the other.
Aggregation:
- Definition: Aggregation is a specialized form of association where one class contains objects of another class, but the contained objects can exist independently.
- Dependency Level: It represents a slightly stronger relationship than association, but still relatively weak. The objects are related, but they can exist independently.
- Example: Consider classes
Department
andEmployee
. A department can have multiple employees. If the department is closed, employees can still exist independently.
Composition:
- Definition: Composition is a strong form of association where one class contains objects of another class, and the contained objects cannot exist without the container class.
- Dependency Level: It represents a strong relationship where one class (the container) owns the other class (the component). The lifecycle of the contained object is tightly bound to the container.
- Example: Consider classes
Car
andEngine
. A car is composed of an engine. If the car is destroyed, the engine is also destroyed or becomes meaningless.
In summary, the key differences lie in the strength of the relationship and the dependency between the classes. Association is a generic relationship, aggregation is a weaker form of association where objects can exist independently, and composition is a strong form of aggregation where objects cannot exist without the container class. The choice between these relationships depends on the design requirements and the level of dependency you want to establish between the classes in your Java application.