Aggregation is a type of association that represents a "has-a" relationship with a
whole-part hierarchy.
In aggregation, the child object can exist independently of the parent object,
implying a weak relationship between the parent and child.
Aggregation allows one class to contain another class without owning its lifecycle.
What is Aggregation:
--------------------
Aggregation is a special type of association that represents a whole-part
relationship where the child (part) can exist independently of the parent (whole).
It is used to model relationships where the contained objects are not strongly
dependent on the lifecycle of the container object.
What is Composition:
---------------------
Composition is a design principle where a class contains references to one or more
objects of other classes. This allows the class to use the functionality of the
composed objects and delegate tasks to them.
Composition is often preferred over inheritance because it promotes greater
flexibility and modularity in the design.
Example: A university consists of several departments.
Whenever a university object is destroyed automatically,
all the department objects will be destroyed.
Without an existing university object, there is no chance of an existing
dependent object; hence, these are strongly associated, and this relationship
is called composition.
Негізгі бет OOPS :Has-a Relation::Aggregation and composition
Пікірлер