Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4097
  • Last Modified:

association vs aggregation vs inheritance vs composition

associations, aggregations, and inheritance


Hi,

I always get confused with these three structural relationship terms used heavily in java.

a.associations
b.aggregations
c.inheritance
d. compostion

What are the practical uses of them. Why we need them and which one to use in which case. Specifically association vs aggregation is more confusing to me. when to use aggregation and when to use plain association in the UML representation. what is the practical difference between uni and bi directional association and what are the various symbols used to represent them in UML. How to prepare association matrix of all the objects with each other involved during analysis phase. what are the static and dynamic aspects of abstraction?
Please advise
0
gudii9
Asked:
gudii9
  • 5
  • 4
1 Solution
 
dpearsonCommented:
That's a lot of questions - I'll try to bite off a piece.

Part of the confusion you may be having between the terms is that "Association", "Aggregation" and "Composition" are all forms of the same thing but with progressively tighter requirements.

Association just means two objects are related.  If A has a reference to B then they are associated.

Aggregation and Composition are very similar to each other.  They both indicate that A contains B - not just that it has a reference to B.  The difference is subtle and only matters if you're trying to be very precise with your terms.  Technically 'composition' is when the parent A uses a purely internal B.  In 'aggregation', the B object (the child) might be created externally or shared beyond the parent object.  It's still part of A, but it may be accessible by others.

To explain this with an example - the relationship between me and my heart is composition - when I was created, my heart was created too and it's internal to me (nobody else interacts with my heart).  The relationship between me and my shoes is aggregation.  They are my shoes (nobody else owns them) but they were made at a factory and given to me.  But me, my heart and my shoes could all be seen as part of a "Person" object.  As for my friends - those I have an association with.  They are not part of me.

Again - this is a small difference.  You can ignore this difference between aggregation and composition in most cases without problems and use either term in most situations.

If you'd like to read up more about this hierarchy this is a good summary of the differences:
http://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/

As for your questions about UML - I can't help there.  I neither know nor wish to know anything about UML :)

Doug
0
 
gudii9Author Commented:
I can relate better now and I see thick/filled in/black diamond arrow as the composition relationship is thick where as aggregation it is open/white/thin arrow as relationship is thin and open extended triangle for inheritance pointing towards base class(may be because inheritance one form of extension of base class). In the shoe example what is the direction( it the Person contains the Shoe right even though it could be external and not as restrictive as Person, Heart).

Also what is uni and bi directional association.

what are the static and dynamic aspects of abstraction.

How the genaralisation is related to inheritance and 'is a' relationship'

So composition, aggregation is related to to 'has a' relationship right.

Association could be both 'is a' as well 'has a'.

How 'association' is different from 'dependency'

I read inheritance implies it is relationship between the classes but not between objects. And makes not sense to apply multiplicity on inheritance.
Not sure what exactly what above two lines means? All others(like association, composition, aggregation etc) are relation between objects but not classes?

please advise
0
 
dpearsonCommented:
Glad this is making more sense now.  You have about 10 new questions - all of which are big topics.  I can't take them all on in this one question - we'll never get to the end :)

I'd suggest breaking some of those out into new questions.

This one:
Also what is uni and bi directional association.

Is simple enough to answer here.

Unidirectional means A has a reference to B.  But B does not have a reference to A.
Bidirectional means A has a reference to B AND B has a reference to A.

Doug
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sharon SethCommented:
How the genaralisation is related to inheritance and 'is a' relationship'

HashMap 'is a' Map .  

Map is a generalised form of HashMap .
0
 
gudii9Author Commented:
what is 'has a' in this context. please advise
0
 
dpearsonCommented:
"has a" means the parent object contains a reference to the child (so it's using composition or aggregation).

E.g.
   Person Has-A Leg
   House Has-A Door

Unlike "is a" which means "is a kind of" (so we're talking inheritance).

E.g.
    Teacher Is-A Person
    Child Is-A Person

Doug
0
 
gudii9Author Commented:
In the java world inheritance means both 'Extends'(class A extends class B) and 'implements'(classA implements interfaceB right.

How the composition or agregation of parent object containing reference to child represented in java world(not in UML world)
Please advise
0
 
dpearsonCommented:
In the java world inheritance means both 'Extends'(class A extends class B) and 'implements'(classA implements interfaceB right.

Yes.  If you want to be more precise then "extends" is "implementation inheritance".  While "implements" is "interface inheritance".

How the composition or agregation of parent object containing reference to child represented in java world(not in UML world)

Just a class with a reference to an object:

// Composition:
// People have legs and if you delete the person, the legs are deleted as well.
public class Person {
     private Leg m_LeftLeg = new Leg() ;
     private Leg m_RightLeg = new Leg() ;
}

// Aggregation:
// Employees have supervisors.  But if you delete the employee the supervisor remains.
public class Employee {
      private Supervisor m_MyBoss ;
}

Doug
0
 
gudii9Author Commented:
so composition and aggregation we are talking within the class where as implementation inheritance/interface inheritance" we are talking about interaction of a class with outside entity(class or interface ) right?

so be a composition and aggregation or  implementation inheritance or interface inheritance everything comes under association right since there is relationship?

Please advise
0
 
dpearsonCommented:
so composition and aggregation we are talking within the class where as implementation inheritance/interface inheritance" we are talking about interaction of a class with outside entity(class or interface ) right?

so be a composition and aggregation or  implementation inheritance or interface inheritance everything comes under association right since there is relationship?

Yes that's all correct.

Doug
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now