Difference between Association, Aggregation and Inheritance relationships.
An Association is a relationship between similar objects or objects with common structures.
Example:
An employee object is associated with manager object.
Aggregation is “a part of” relationship.
Example:
A student object is a part of College object.
Inheritance is a “is a “relationship where one or more classes are derived from a base class. The derived class has the base class plus its own characteristics.
Example:
French_Student class can be a derived class of student class.
An employee object is associated with manager object.
Aggregation is “a part of” relationship.
Example:
A student object is a part of College object.
Inheritance is a “is a “relationship where one or more classes are derived from a base class. The derived class has the base class plus its own characteristics.
Example:
French_Student class can be a derived class of student class.
Aggregation vs composition vs inheritance vs association vs dependency
Going to try and explain what the difference between the three is in plain English fully aware of what passions are inflamed by the subject!
Aggregation
Aggregation is illustrated using an unfilled diamond and an arrow pointing to the member.
Composition
Composition is illustrated with a filled diamond and an arrow pointing to the component.
Expressed in code:
To explain the difference between composition and inheritance further the person has an insurance number, however without the insurance number the person is still a person. However a person can no longer be a person without a head (someone is bound to point me towards a case that disproves this assertion now…)
Inheritance
Inheritance is illustrated with an empty triangular arrow pointing from the subclass to the super class. I’ve also shown the implementation of an interface on the right hand side.
Expressed in code:
So the difference between inheritance and composition should be pretty clear from the diagram above. A person is a living thing, a head is not a person it is part of a person.
Directed Association
The arrow denotes that the person and tax company are using a car, the car is not using the person or the taxi company. The car doesn’t care that the person or taxi company exist.
Bidirectional association
A man is a husband to a woman, a woman is a wife to a man.
Dependency
The Job and the Person are two distinct entities that do not exist because of one-another, ruling out aggregation and composition. A change to the job may change the person – they become wealthier or might move to another country if the job moves making this different to the relationship between the car and the person where change to the car do not result in a change to the person.
33333
This is very basic stuff we use in our daily coding life but we are not aware of these concepts . Let me explain you :
Objects always have some relationships and association, composition and aggregation is all about relationships.
Inheritance: It's Is A relation : Parent-child relationship. e.g :Saurabh is a business manager of some company, Beer is a Alcohol. So in both examples we have is a relationship like in first example : Employee would be a base class and manager will be child class and in the second example Beer would be a child class for Alcohol.
Conclusion : Inheritance is nothing it's just Is A : parent child relation ship
Association: It's Has a relation : In simple words : I have a Watch. There is a relation ship between Me and my watch but we are not dependent on each other. I can exist without my watch and my watch can exist without me so this is called Association.
Let me explain this concept with code :
This is very basic stuff we use in our daily coding life but we are not aware of these concepts . Let me explain you :
Objects always have some relationships and association, composition and aggregation is all about relationships.
Inheritance: It's Is A relation : Parent-child relationship. e.g :Saurabh is a business manager of some company, Beer is a Alcohol. So in both examples we have is a relationship like in first example : Employee would be a base class and manager will be child class and in the second example Beer would be a child class for Alcohol.
Conclusion : Inheritance is nothing it's just Is A : parent child relation ship
Association: It's Has a relation : In simple words : I have a Watch. There is a relation ship between Me and my watch but we are not dependent on each other. I can exist without my watch and my watch can exist without me so this is called Association.
Let me explain this concept with code :
public class Calculator { //Addition private int Add(int x, int y) { Add objAdd = new Add(); return objAdd.Addition(x, y); } //Subtract private int Sub(int x, int y) { Sub objSub = new Sub(); return objSub.Subtraction(x, y); } }
public class Add { public int Addition( int x, int y) { return x + y; } } public class Sub { public int Subtraction( int x, int y) { return x - y; } }
In the above code we can easily see Class Add returns sum of two numbers and Class Sub returns subtraction of two numbers. So all the three class exists independently. When we create object of Calculator class and will access the Add method then only we will create object of Add class. So this is called Association.
Aggregation : It's Has a relation with one owner (Association and aggregation are almost same with just one difference, In aggregation we have one owner but in association there is no owner) . Let me give you some example :Company has employees and they work under CTO.
So all employees should work under CTO and CTO is the head of employee.CTO will be the parent of all level of employees.Let us discuss this through code :
public class CTO { List<Employees> listOFEmployees = new List<Employees>(); public Employees GetEmployeeName(string employeeName) { foreach( Employees emp in listOFEmployees ) { if(emp.EmployeName == employeeName) return emp ; } } } public class Employees { private string employeeName = string.Empty; public string EmployeName { get { return employeeName ;} set { employeeName = value;} } }
So basically Employees class can have it's own life time but it always work under CTO or Manager. So Manager or CTO will always be the parent class for all employees. Life time is not dependent .
Composition : Again It's Has a relation but it's a specialized form of Aggregation : I have a finger/hand. There is a relation ship between Me and my finger/hand and we are dependent on each other. I can not exist without my hand or finger and vice versa so this is called composition. Child object doesn't have their life cycle if parent object deleted. So no independent life of objects can exist.
Let us discuss this through the code :
public class Body { BodyParts objBodyParts = new BodyParts(); private Arraylist CalculateParts() { Arraylist objArraylist = new Arraylist(); objArraylist.Add(objBodyParts.CalculateFingers()); objArraylist.Add(objBodyParts.CalculateHands()); } } public class BodyParts { public int CalculateFingers() { return 10; } public int CalculateHands() { return 2; } }
In the above example we can easily see that life cycle of BodyParts and Body goes parallel. If object is body gets deleted then BodyParts also not exist .
No comments:
Post a Comment