Middle
What is aggregation and composition?
sobes.tech AI
Answer from AI
Aggregation and composition are types of relationships between objects in object-oriented programming.
-
Aggregation is a "part-whole" relationship where the part can exist independently of the whole. For example, a "Team" class contains "Player" objects, but players can exist without the team.
-
Composition is a stronger relationship where the part cannot exist without the whole. For example, a "House" class and its "Rooms": if the house is destroyed, the rooms also disappear.
The main difference is the lifetime of objects: in composition, the lifecycle of the part depends on the whole, whereas in aggregation, it does not.