Association, aggregation, and composition in OOPs

Bindu C
5 min readMay 5, 2023

#Composition #Aggregation #Inheritance

Article Outline:

  • Association(using relationship): The objects created and destroyed independently and represented as one-to-one, one-to-many, or many-to-many (also known as cardinality).
  • Aggregation(HAS-A relationship): parent can exist without child and a child can exist independently of the parent and this involve a one-to-one, one-to-many, or many-to-many relationship between the participating objects.
  • composition(“death” relationship, PART-OF): parent destroyed, child dies
  • Inheritance : is-a relationship.

There are many different types of relationships which can exist between two or more classes. There are totally five different types of relationships: association, aggregation, composition, dependency, and inheritance.

The most common two types are:

  • Inheritancean “is a” relationship
  • Associationa “has a” relationship

Inheritance:

  • It mechanism that allows us to take all of the properties of another class and apply them to our own.
  • The parent class is the one from which the attributes and functions are derived (also called as Base Class). Child Class refers to a class that uses the properties of another class (also known as a Derived class). An Is-A Relation is another name for inheritance.
class Person:
def __init__(self, fname, lname, address):
self.fname = fname
self.lname = lname
self.address = address

def display(self):
print("First Name: ", self.fname)
print("Last Name: ", self.lname)
print("Address: ", self.address)

class Student(Person):
def __init__(self, fname, lname, address, age, gradYear):
super().__init__(fname, lname, address)
self.age = age
self.gradYear = gradYear

def display(self):
super().display()
print("Age: ", self.age)
print("Graduation Year: ", self.gradYear)

# person object
per = Person("Adam", "Ho", "1234 abc blvd")
per.display()
print("===========================================")
std = Student("Peter", "kee", "9876 xyz blvd", 28, 2008)
std.display()

Association:

The objects that are part of the association relationship can be created and destroyed independently.

  • Association is a semantically weak relationship (a semantic dependency) between otherwise unrelated objects.
  • An association is a “using” relationship between two or more objects in which the objects have their own lifetime and there is no owner.
  • Composition and Aggregation are the two forms of association.
Composition is defined by the PART-OF relationship which means that one object IS PART-OF ANOTHER OBJECT, but Aggregation is defined by the HAS-A relationship which means that one object HAS-A RELATION with another object.

Example: An association relationship between two classes is a “has a” relationship. For example:

  • A Car has an Engine and a Wheel
  • A Person has a Leg and an Arm
  • A Book has Pages
  • A doctor has many patients and patient can consult many doctors

(Each of these objects has its own life cycle and there is no “owner” or parent) ie.

  • ClassA contains ClassB as an attribute, or
  • Instances of ClassB are constructed inside ClassA

Aggregation:

  • Aggregation is a specialized form of association between two or more objects in which each object has its own life cycle but there exists an ownership as well.
  • An essential property of an aggregation relationship is that the whole or parent (i.e. the owner) can exist without the part or child and vice versa.
  • The life cycles of parent objects and child objects are independent. In a composition relationship, the death of a parent object also means the death of its children.
  • examplA department can have students but vice versa is not possible and thus unidirectional in nature.
  • In Aggregation, both the entries can survive individually which means ending one entity will not affect the other entity.

composition:

  • If the parent object is destroyed, then the child objects also cease to exist.
  • The life cycle of the part or child is controlled by the parent that owns it.
  • Control can either be direct or transitive. That is, the parent may be directly responsible for the creation or destruction of the child or the parent may use a child that has been already created. Similarly, a parent object might delegate the control to some other parent to destroy the child object.
  • As an example, a house may be composed of one or more rooms. If the house is destroyed, then all of the rooms that are part of the house are also destroyed. The following code snippet illustrates a composition relationship between two classes, House and Room.

In both aggregation and composition, an object of one class can be the owner of an object of another class. And in both aggregation and composition, the child objects belong to a single parent object, i.e., they may have only one owner.

Why we should use Aggregation over Composition?

Let’s take an example of both Composition and Aggregation to see the difference between them, and understand both precisely.

  • In the case of Composition, if we delete the object emp then the object of the Salary class which we initialized inside the EmployeeOne class will be deleted automatically because it is completely dependent upon the object of EmployeeOne class, so it might cause some error in the output code.
    But in the case of Aggregation, we can see that we have created completely two different objects emp and salary, and passed the salary object as a parameter to the EmployeeOne class, so even if we delete the emp object, the object of the Salary class will remain the same and we can also use that elsewhere.
  • In the case of Composition, the objects were interdependent on each other, but in Aggregation, they are Unidirectional means that as salary is a completely independent object we can only pass salary to the emp, not vice versa.

--

--