Relationships in Object-Oriented Programming
Tags: python, programming
Published: 2023-02-21 22:11:48

So far so good, I talked about [abstraction and encapsulation](https://dev.to/meistens/public-and-private-interfaces-in-oop-1fpn) 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
1. Composition
2. Inheritance
3. 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](https://i.postimg.cc/Bb0stbkj/UMLClass-Of-Objects-excalidraw.png)](https://postimg.cc/dk4gxsqF)

## 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](https://dev.to/meistens/public-and-private-interfaces-in-oop-1fpn). 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](https://i.postimg.cc/Z5pkgpqX/UMLComposite-Aggregate-excalidraw.png)](https://postimg.cc/rDVYRdx9)

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.