31 How to Implement Composition and Aggregation in python Programming?

Опубликовано: 25 Март 2026
на канале: Free Tutorial TV
17
1

Now if you haven't watched my last video in which I have shown you how to use class composition in python.

I will highly recommend you to watch that video. because I will be using the same code to demonstrate . how aggregation works in python.

So in the last video we have created two classes.

One is an Employee class and the other is the Salary class.

And then we have used the Salary class inside the Employee class . and we have seen how composition works

in python.

So Employee class is delegating some part of its responsibility to the Salary class and that is called

the composition . Now in composition The relationship between the classes, which have some association is represented by a keyword part of

so salary is the part of employee.

So the key word we use in composition is part of . let's see how we can use aggregation instead of

composition between these two classes.

So let's say I will not use this Salary class inside the Employee class.

But what I'm going to do is . I'm going to boss an instance of salary here. so I'm going to just write

salary . and this will be the object of salary class. which I'm going to pass init method of the

Employee class.

So let's create an object of the salary.

So I'm going to just write salary here. which is equal to the salary class. and we will instantiate this salary

class by these two argument . which we have given in the last video to the Employee class.

So I'm going to just copy these two arguments and. paste it here and I'm going to remove this comma here.

So now what we are doing here is we are first of all instantiating the Salary class . and now the second

class we can pass to the constructor of the Employee class.

And this is the object which we have parsed using does salary class object.

And then I'm going to take the Salary class object and I'm going to assign the Salary class object to

the member variable of the Employee class which is O B J underscore salary.

**************************************************************************
please like and follow us in Facebook and Instagram.
facebook:   / technologytv  


Instagram:   / heikhamsingh  


Twitter:   / hanandkumarsing