What's the difference between composition and aggregation

What do the expressions “is” and “has” mean in terms of OOP principles?

"Is" implies inheritance. "Has" means an association (aggregation or composition).

What's the difference between composition and aggregation?

Association denotes a relationship between objects. Composition and aggregation are special cases of the “part-whole” association.

Aggregation assumes that objects are connected in a "part-of" relationship. Composition is a stricter variant of aggregation. In addition to the “part-of” requirement, the condition is imposed that the “part” instance can only be included in one whole (or not included anywhere), while in the case of aggregation, the “part” instance can be included in several ones.

For example, a book consists of pages, and we cannot rip a page out of a book and insert it into another book. Pages are clearly linked to a specific book, so this is composition. At the same time, we can take and transfer a book from one library to another - this is already an aggregation.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed