DEV Community

Cover image for Relationships in Object-Oriented Programming
David Mebo
David Mebo

Posted on

Relationships in Object-Oriented Programming

So far so good, I talked about abstraction and encapsulation in my previous post, but we don't know yet how to use it to create proper levels of abstractions. There are ways to go about this, and they are called relationships. They are:

  1. Association
  2. Composition
  3. Inheritance
  4. Aggregation

Association

Association is a type of relationship that has that "in a relationship with" relationship. It establishes a relationship between two classes using their objects.
Take this UML diagram below, it shows two classes connected by a line. That line in UML is called an Association, and it fits the use case.

UMLClass-Of-Objects-excalidraw.png

Composition

Composition is the process of collecting several objects together for the purpose of creating a new object.
An example of composition can be seen in the car example of abstraction posted last week. The car object provides the interface needed for the driver, while also providing an interface to the mechanic to "fix" broken parts.

Aggregation

Aggregation is almost similar to composition. The difference between them is that aggregate objects can exist independently of each other.
Also, keep in mind that composition is the same as aggregation and aggregation is a more general form of of composition. Also, any composite relationship is also an aggregate one, but never the other way around.

UMLComposite-Aggregate-excalidraw.png

The UML above is a chess set, which shows how composition relationship (represented with a solid diamond) and aggregate relationships (hollow diamonds) works.
In the UML, we will notice that the board and pieces are stored as a part of the Chess set in exactly the same way a reference to them is stored as an attribute on the chess set. This shows the distinction between aggregation and composition is sometimes irrelevant since they behave in the same way.

Inheritance

Inheritance is a relationship of sorts, where a class can inherit from another class or classes that share the same set of attributes and methods with the supposed class inheriting.
In some languages like Java, the class being inherited from, is either called a superclass or base class.
The class that is inheriting from the base class is called the derived class or subclass. The derived class inherits all the properties and methods, or overrides the ones inherited from the base class.

For example, consider a class called Animal that has properties such as name, age, and methods such as eat() and sleep(). A subclass called Cat can be created that inherits all the properties and methods of Animal, and also has additional properties and methods specific to a cat, such as meow() and climb().

Inheritance helps to reduce code duplication, increase code reuse, and promote a more organized and modular programming style.

On that note, hope it made things clear.

Top comments (0)