Updated April 6, 2023
Definition of Aggregation in OOPS
Aggregation in OOPS is defined as a relation that exists between two or more two objects which individually have their own individual life cycle along with the ownership. Aggregation in OOPS constitutes 2 words (aggregation and OOPS). An aggregation is a special form of semantically weak relation which happens between unrelatable objects. Aggregation is one of the 5 types of relationships that exist in a Unified modeling language acronym’d as UML. Now to understand what UML is, in brief, it is the default standard in modeling any object-oriented programming concept. The second word, OOPS, which constitutes the topic of the article is an acronym for Object-Oriented Programming Structure which is a programming structure that relates data into objects and rather than looking into function and logic. OOPS takes into account that developers are interested in the manipulation of the object rather than the logic of manipulating them and is an essential fundamental for any collaborative development.
There are various coding languages that implement OOPS like Python, Java, etc. For the uniformity of the article, we will take into account Python language for understanding the concept of aggregation in OOPS.
Declaration of class in Python:
class <class name> : def __init__(self, <value 1>): self. <variable 1 > = <value 1>
Defining a function in Python:
def <function name> (self): return (|| operation done on self.<variable 1> ||)
Here, the < function name > is replaced by the name of the function and the name should signify the intent of the function. || operation done on self.<variable 1> || is the operation that is acted on the < variable 1 > which yields the variable to be returned. As an example, if one needs to return the yearly salary of an individual, given the monthly salary, the variable should be multiplied by 12 and then returned.
Instantiation of the class variable:
<object variable> = <class name>(<arguments>)
Here, the < object variable > denotes the variable name that will be used across the codes as an instantiated variable of the class. The < class name > denotes the class name that is declared as a part of the algorithm and structure of the application. < arguments > denotes the arguments that need to be passed to the init function of the initialization of the object of the class.
How does Aggregation work in OOPS?
Aggregation as a concept comes in handy when it is difficult to have a concept of inheritance in OOPS. Suppose there is a condition where the classes might not have a parent-child relationship and rather have a peer relation and in those cases concept of aggregation works the best. For example, the universal example of simple understanding, an employee use case. For an employee, the salary is one of the components, but the class salary doesn’t have a parent-child hierarchy and has a “part of” or “has a” relation. We can tell that salary is a “part of” employee or maybe employee “has a” salary. Now, these 2 components differ big time as “part of” relation signifies composition and “has a” relation signifies aggregation.
To understand the concept of aggregation we would need to know the type of relation the classes have amongst themselves. From the earlier paragraph, we understand that in aggregation employee “has a” salary, and hence we can pass the class instantiated variable of class # 2, in a class # 1 “has a” class # 2 relation. The same object of class # 2 can be used for calculation in class # 1 and this variable can be used for instantiating the “self” variable in class # 1. At first, we would have to instantiate class # 2 and store the object in a variable. this object variable is passed as a parameter in the other class # 1 where we would need to perform operations as per the requirement of the application. The class object that is passed to class # 1, will be instantiated as a variable of class # 1. Once this object is instantiated, one can use any other related function of the class variable to perform the task.
Using our classic “Employee” example (the hands-on example will be presented in the next section), we first define a class salary which has components like, monthly basic, monthly flexible component, yearly bonus, and a yearly PF investment. We will define a net CTC function that will calculate 12*(monthly basic + monthly flexible component) + (yearly bonus and a yearly PF investment) and return the same. Now, we will also have an employee class that will contain the name, employee ID, and the object variable of the salaried class. We will also define another function where we will use the salary object variable’s inbuilt function to finally return the CTC of the employee. In the main code, we will first instantiate the salary object with the required parameters and then pass this to the employee class. Now in the employee class, the variables are passed, it goes through the instantiation and when the total CTC function in the employee function is called it, in turn, asks the execution of the salary function, which is the sub-call in that function, and finally, the value is returned.
In the example section, we will try to grasp different elements of aggregation and will comment on some elements explicitly for better understanding.
Example of salary-employee aggregation concept
class Salary: def __init__(self, basicPay, flexiPay, bonus, PPF): self.basicPay = basicPay self.flexiPay = flexiPay self.bonus = bonus self.PPF = PPF def get_ctc(self): return ((self.basicPay+self.flexiPay)*12+self.bonus+self.PPF) class Employee: def __init__(self, name, empId, salaryObj): #The salary object is passed self.name = name self.id = empId self.salObj = salaryObj #The salary object is instantiated def annualCTC(self): #Calling the get_ctc function of salary class return "Total salary for " + str(self.name) +" with Employee ID " + str(self.id) + " is: " + str(self.salObj.get_ctc()) obj_sal1 = Salary(600, 1000, 10000, 3000) obj_emp1 = Employee("Employee 1", "270991", obj_sal1) print(obj_emp1.annualCTC()) obj_sal2 = Salary(600, 1000, 0, 3000) obj_emp2 = Employee("Employee 2", "270919", obj_sal2) print(obj_emp2.annualCTC())
The net CTC of employee 2 should be 10000 less than employee 1 as only the bonus component is different between them.
In this article, we looked at the working of aggregation in python (one of the languages that implement OOPS) and saw that if we pass the object of a class to another class we can have the “has a” relation between them and the function of the sub-class can easily be implemented.
This is a guide to Aggregation in OOPS. Here we discuss the Definition, syntax, How Aggregation works in OOPS? examples with code implementation. You may also have a look at the following articles to learn more –