1.

Explain Aggregation vs Composition in Java.

Answer»

An association is a relationship that exists between two distinct classes and is established through their Objects. One-to-one, one-to-many, many-to-one, and many-to-many associations are all possible.

An Object communicates with other Objects to leverage the capabilities and services provided by that object in Object-Oriented programming. Association has two forms and they are composition and aggregation.

Aggregation is a unique type of association in which:

  • It denotes a Has-A relationship.
  • It's a one-way relationship or a unidirectional association.
  • In Aggregation, both entries can PERSIST independently, IMPLYING that terminating one entity will have no influence on the other.

In the above image, we can clearly see that each teacher is associated with a college, and each student is also associated with a college. Thus, both the entities, the student and the teacher can exist independently. 

Composition is a type of Aggregation in which two entities are extremely reliant on one another.

  • It denotes a part-of-relationship.
  • Both entities are reliant on one another in composition.
  • When two entities COME TOGETHER to form a composition, the composite object cannot exist without the other.

In the above image, we can clearly see that both TYRE and Engine form an integral part of a vehicle. Both are reliant on each other. Without either of them, the Vehicle entity is useless.



Discussion

No Comment Found