What is aggregation and composition in Java with examples?
Herein, what is aggregation and composition?
In both aggregation and composition object of one class "owns" object of another class. But there is a subtle difference: Aggregation implies a relationship where the child can exist independently of the parent. Composition implies a relationship where the child cannot exist independent of the parent.
Also asked, what is aggregation explain with example?
Aggregation is a way of composing different abstractions together in defining a class. For example, a car class can be defined to contain other classes such as engine class, seat class, wheels class etc. Other examples of aggregation are: A window class containing menu class, check-box class etc.
Aggregation is having a bunch of objects in another object. For example, you aggregate a bunch of items in an ArrayList. Segregation typically deals with separation of concerns. For example, you segregate presentation from the view in MVC.