General

Object Relationships

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

  • #theory
  • #object

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

Notation

In a class diagram that is representing the inheritance relationship, if the parent is an abstract class, the class name is italicized.

Realization

Similar to inheritance, realization refers to the implementation of an interface instead of inheriting from another class.

Example:

  • The animal here is an interface and it encapsulates certain behaviours that an animal has. The cat behaves like an animal.

"Behaves like" or "can do" is the keyword for realization.

Diagrams showing the realization relationship between cat and animal interface

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