Object Oriented programming ( OOP ) :- What is Aggregation , Association and Composition ?

In this video we will try to understand three important concepts Three important concept from object oriented programming aspect association aggregation and composition The most exciting feature of a object oriented programmings that it mimics real world objects It tries to code replicates real world objects like customers, supplier etc When we talk about real world objects the most complicated thing in real world is relationships Take this simple real world requirement which you see in your screen This requirement is just having 5 sentences in it The first sentence which says a manager is a type of employee Manager has a swipe card by which he enters a company premises He has many workers under him His salary depends on project success and the project success depends on a manager We have five point requirement here In this five point requirement you can easily observe lot of relationship Start with line one just number them The first requirement says a manager is a type of a employer. Put a small bracket over this two letter word here which is very important to depict relationships When we say a manager is a type of a employee it means that It forms a parent child relationship We are going to have a top employee class and that employee class will be inherited to create a manger class In other words the employee is a parent of manager The first kind of relationship which you can easily see from the first statement is its a parent child relationship. Its inheritance relationship Whenever you find this (is a) word it means you need to probe ahead to see in case if there is any kind of inheritance or a parent relationship Whenever you find a word (is a). For example Shiv is a son of his father or like manager is a type of employee. Employee becomes a parent and the manager becomes a child Look at the second requirement Manager has a swipe card to enter the company premises Put a bracket here saying around the word(has a) This (has a) word actually shows a different kind of relationship The manager uses a swipe card to enter the company premises It also means that swipe card can exist without the manger and the manager can exist without the swipe card In other words take that swipe card and give it to some other employee The manager can take a new swipe card Swipe card and manager they are not really dependent on each other they can work in isolation They come together for solving some problem or for solving some purpose This kind of relationship which you can easily see from this word (has a) is called as the association reltionship The association relationship Both this real world objects can stay without each other. They have there own lifetime must be the swipe card can tomorrow be broken But the manager will leave it doesn’t mean that the manager also dies And also the second thing the manager resign from the company and go away and that swipe card still be assign to some other employee The swipe card and the manager both of them can stay without each other. This kind of relationship is called as the association relationship Look at the third requirement manager has many workers under him The manager is like a owner when I say owner he is responsible to ensure that the task are properly assigned to workers etc Workers report to the manger These workers will not report to accountants they will not report to some other entity in the company They will only report to manager. In other words the manager is the owner of these workers and workers cannot belongs to some other entity in the company This kind of relationship is called as the aggregation relationship. In aggregation relationship the child object or there is a single owner of the child object These child objects cannot belong to any other owner That is called as a aggregation relationship These child objects which are inside the owner can exist independently in other words the worker objects can exist independently. I can The worker can move happily in the company From the job prospective he has to work inside the manager This kind of relationship is called as a aggregation relationship where we have only one owner for the objects but the objects can stay happily without the owner Take the fourth and the fifth requirement Will take this one requirement in one go because both this requirements depict only one relationship The requirement fourth says manager salary depends on projects success and the requirement five says project success depends on how good the manager works or good the manger manages the project The project will be successful if the manager works properly The manager salary will be incremented if the project is successful The project and manager are depended on each other We cannot stay without each other The lifetime of the project as well as the manager is same If the project is running well the manager is doing well The time the project goes under issues the manager will have his own issues of salaries In the same way the project will be doing well if the manager does well Both these objects are dependent on each other This kind of relationship is called as the composition relationship The manager needs a project and a project needs a manager, both are composed of each other In composition relationship The lifetime both the objects are same. In other words if the project goes off the manager will have problem the manager goes off the project will not be successful we have four kind of relationships Inheritance is the parent child relationship Association where both the objects use each other but they can have the own life time Aggregation where there is a single owner of the object and composition where each object depends on each other If one goes of then the other will also go off The lifetime of the objects are same We have understood this four important relationships. How about going and writing some C# code to see how they come up We will not be writing the code for the inheritance because we have already discussed this principle in the one of those videos in the OOPS questions and answers series concentrate on association, aggregation and composition in this video Start visual studio First start with association Create a new project Create a class library called as companies Companies has employee it has projects Create a nice class library here called as Organization The first class I will create is the manager class The next class is Swipe Card is a class which does all the functionality for a swipe card This swipe card class has method called as swipe This swipe method goes and takes in a manager It goes swipes the manager In the same way the manager comes in the morning to logon he needs a swipe card With this he swipes himself They both use each other This will return the manager name. In the same way I can have some other functions in swipe card get the make of the swipe cards which company does this swipe card belongs to This gives me the manager name “Shiv” There is a manager class, swipe card class The swipe card class uses the manger as well as the manager class uses the swipe card to swipe. There is no single owner as such Add a very simple console application here Reference the organization library which I have created. I will add a reference of organization library This is a very important step for association I can create the object of manager I can create the object of swipe card There is no single owner of these objects Both use each other in other words if I say objManager. Logon then I need a swipe card object here In the same way if I say objSwipe.Swipe then I need the manager They still can exist separately independent of each other none of them are owner of each other They have separate entities all together and they both have their own lifetime If I build this project, both these objects can stay without each other. If I build it there are no compilation errors I can use this manager object and say objeManager get me the manager name It return me the name of the Manager, in the same way I can get make of the swipe card without even worrying about the manager I can get the make of the swipe card Both these objects can stay without each other. They come together when they want to swipe For all other operations they do not need each other This kind of relationship is called as the association relationship where there is has two characteristics one is there is no parent object or owner Second the lifetime of both the objects are different The aggregation requirement Manager has many workers under him Go to our organization library here Create a worker class I have only one property for now which will give out the worker name The manager is the owner of the workers in other word the worker only belong to manager. In other words the manager will have list of workers This depicts a aggregation relationships The workers belong to manager The manager is the owner of these workers Still the workers can be created separately means we can create the object of worker without thinking that I need a manager means I can always use a worker if I wish It will get the worker name In this relationship you have one and only one owner The manager is only one owner of the worker In other words I cannot imagine the swipe card is a owner of workers That is not possible This worker can only belong to the manager This kind of relationship is called as the aggregation relationship Coming to the final one The composition relationship We need to do bit of a coding here The salary of the manager depends on the project success and the project success will depends on how good the manager is I am going to create a class called as project I will have a property called as a public boolean IsSuccess This success depicts if the project is successful or not I will also create one more property called as the Salary property Currently I am not creating any encapsulated properties with set and get For simplicity I have made it a public variable but as a good practice you would like to have this encapsulated rather than make it like this For simplicity I am creating properties as a straight forward make it public property The manager will have property called as salary If the project is successful then the manager salary can be incremented If the manager is working properly then the project is successful If I say public void How is the manager If the manager is good I’ll say if good This project actually will be needed inside this manager object If the manager is good then this project is going to successful Or else its going to be false If the manager is good this project is successful or if the is unsuccessful In the same way if the project is going to be successful over here that means his salary will be good It also means that I need to have in this project a manager class Here is a constructor, to create a constructor I am saying inside a constructor I am passing the manager class Whenever the project he has to pass in the manager object so that he can set his salary properly I will also make this as encapsulated If this is going to be a successful somebody sets this success is true This value is true Please go and set salary of the manager go and increment the salary of the manager When I create this project class I need to give him the manager class because the manager state is dependent on how well the project is doing If this manager is good he will say the project is successful If the project is successful if I go down below here Then I will increment the managers salary if the project is unsuccessful then I am going to decrement the managers salary If I try to create the object of the manager class and the first thing if I try create the object of the project class class It needs a manager class In the same way when I talk about manager, when I say this manager is going to be how is the manager, if I say it is good It needs the project class without that also it will not help out How both these components, the manger as well as the project depends on each other If the manager is good then the project is successful and he will increment the salary This is called as the composition relationship where the manager cannot really work without the project class. In the same way you cannot create the object class without the manager class This kind of relationship is called as a composition relationship. There are three kinds of relationship Association relationship where basically both the object lifetime different there is no single owner Aggregation relationship where we have a single owner but the child object can still have a different lifetime they can exist independently Then we have the composition relationship and some people also call this relationship as the death relationship where both the components dependent on each other If one component dies off then the other component also goes off For exampl if the manger goes off then inside the manager this project will also go off that will also not exist. In the same way I cannot create the object of the project class without the manager I hope you enjoyed this video, in this video we saw three important concepts Association, Aggregation and Composition Thank you very much

  1. I found this a great explanation. Putting the different concepts side by side in the same example made it easy to follow. Thanks.

  2. Hi questpond
    I don't think there is a better way of explaining OOP than you have done. Thank you very much for making a perfect video like this.

  3. all your explanation is like a literature class.. It is not technical explanation.. I dont understand this thing..

  4. Hi, Friends Its Shah Hasnat Developer…… Well I went through some comments against the Flaws in the Composition Section of this Video .Well I modified it . See Code Below  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //using ClassLibrary1;
    namespace ConsoleApplication1111
        class Program
            static void Main(string[] args)
               // double aa;
                Manager obj = new Manager();
               // Project pr = new Project(obj);
               // obj = pr.ret();
               // Console.WriteLine(obj.salary);

        public class Project
            public Manager Manager_object_and_Projectmember; // Declared Other's Composite Class Object to Paralyze Both Classes so that their life time depends on each other
            public bool success_sign_T_F;

            // Constructors :.
            public Project(Manager obj)
                Manager_object_and_Projectmember = obj;
                // Paralyzing the Both Classes in Constructor Level
                // Project Constructor Doing nothing for Its class Volutarily, But for controlling Composite object
            public void Success_sign_T_F(bool value)
               // get { return success_sign_T_F; }
               // set { success_sign_T_F = value; 
               if (value==true) { Manager_object_and_Projectmember.salary = 1000; Console.WriteLine("Increased"); }
                else { Manager_object_and_Projectmember.salary = 50; } 

        public class Manager
            public double salary;
            public Project Project_object;//  Declared Other's Composite Class Object to Paralyze Both Classes so that their life time depends on each other

            // Constructors :.
            public Manager()
                Project_object = new Project(this);
                salary = 10;
            public void IsEfficient(bool good)
                if (good)
                    Project_object.Success_sign_T_F(true); Console.WriteLine("Efficient");

                else { Project_object.Success_sign_T_F(false); Console.WriteLine("false"); }

           public void show()


  5. Association relationship is "use's a", so the 2nd sentence should be, Manager "use's a" swipe card to enter company premesis

  6. Always people implement a simply association but never implement the way to delete object when parent is destroy. Thanks for your video.

  7. Great job!
    OOP is not something you learn over the week end and then just use.
    This video makes perfect sense to me where I'm at in the process.
    Now I need to think about it and come back and watch it again to squeeze some more juice out of it.

    Bonus: if you just take this literally it gets very surreal very fast hahaha!
    It's worth an extra listening just for the shower thoughts:D

  8. I think as per composition " child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted" .
    The example seems to be confusing , it seems more like dependency relationship , where Manager and Project are dependent on each other.
    The Example of composition should be like Company and employee. If Company will be closed there will not be any employee.
    The Life Cycle of employee object is depend on Company Object

  9. He started the requirement with the manager to workers with aggregation, and the salary and the project class with composition,
    but I think his coding turns out in reverse?
    it means from the code, the manager with work class are composition, and the project and salary class are aggregation. ??

  10. In your 1st sentence i think you did mistake about parent child relationship.
    U said that employee class is the parent of manager class
    I think manager class is the parent of employee class.
    Because. Parent table has primary key and the child table has foreign key. Its mean 1 ManagerId can be added in employee table multiple times. Thats simple and clean.
    But from your sight
    If employee class is parent. Its mean 1 EmployeeId can be added in manager table multiple times. Look. How can 1 employee be of many managers or managers have a employee? ? I think you did mistake.

  11. best example for composition is Home and Rooms. Home is coposed of rooms. Rooms dont exist independently. Room needs Home for its existance and Home needs Rooms to be a greate Home. strong Relationship.
    But Agregation is simple. a collection of objects( ex. List of Reportees) and they get associated to an Object (Manager).Relationship is not so tight here.but both are Has-A relationships

  12. Nice video on UML relation. Can u clarify the difference of composition and dependency relationship, the 4 & 5 point are dependent relation i assume

  13. i find this very abstract and sometimes very philosophical. This is kind of open ended interpretation. Is there a clear right or wrong to the interpretation how objects interact each other?

  14. How is that there was no compilation error with C#? There was no forward referencing of Card class. Does C# takes care of forward referencing?

  15. 1)Inheritance Relationship
    Is-A (Parent-Child) Manager (is a) Employee.
    2) Association Relationship
    Has-A(Own Life Time | Exists Independently/Isolation) Manager has a Swipe Card.
    3) Aggregation Relationship
    Has-A(Ownership | Single owner of Child Object | Child Objects can exists Independently) One Manager – Many Workers.
    4) Composition Relationship (Objects depend on each other | Life Time Of both Objects is same) Salary depends on Managers Productivity.

    Thanks for explanations Sir.

  16. i've been googling for hours and still didnt understand the concept until i found this video THANK YOU. I have a midterm tomorrow wish me luck

Leave a Reply

Your email address will not be published. Required fields are marked *