General

Object Relationships

Comprehending the relationships of objects in terms of association, inheritance, aggregation and composition

  • #theory
  • #system

In Object-Oriented concept, a relationship refers to the way in two or more classes interact with each other within a system.

Association

Association represents the relationship between two classes. There are two types of association, namely unidirectional association and bidirectional association.

AssociationExample
UnidirectionalCustomer places an order
BidirectionalA is married to B and B is married to A

Besides, association can also be multiplexed in the form of one-to-one, one-to-many, many-to-one and many-to-many relationships.

Diagrams showing relationships between association, aggregation and composition

Aggregation

Aggregation indicates that when an object that is owned or depend by another object, it can still exist on its own even after the dependent party is dead or ceases to exist.

Examples:

  • The relationship between a car and a wheel. The wheel can still serve its purpose when the car it belongs to no longer exist.
  • The relationship between a company and an employee. The employee can apply for a new company when the old company goes bankrupt.

This relationship can be understood colloquially as "A uses B, where B can live happily without A. "

Diagrams showing the aggregation relationship between car and wheel

Composition

In composition, it is the opposite to the aggregation. The object that is owned by others cannot live on its own.

Examples:

  • The relationship between a cup handle and a cup. It is safe to say that each cup comes with different sizes, colors and materials. Hence, the cup handle is unique to a cup and will have no use outside of the cup.
  • The relationship between a company and their bank accounts. The bank accounts have no use without the company and would be illegal if someone trying to access the account without legitimate authority.

In other words, composition can be understood as "A owns B and B has no meaning outside A. "

Diagrams showing the composition relationship between cup and cup handle

Inheritance

Inheritance describes the relationship between two objects that are usually depicted as a parent and child relationship.

Example:

  • The relationship between a cat and animal. The cat is an animal.

"Is a" is the keyword for inheritance.

Diagrams showing the inheritance relationship between cat and animal

References

Reddy, S. Association vs Aggregation vs Composition.https://www.youtube.com/watch?v=zLvOO4pm6ZI
Association, Aggregation, and Composition in Object-Oriented Programming. Retrieved 2024, March 26 from https://algodaily.com/lessons/association-aggregation-composition-casting