Solved

association vs aggregation vs inheritance vs composition

Posted on 2013-11-26
10
2,611 Views
Last Modified: 2013-12-05
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
Comment
Question by:gudii9
  • 5
  • 4
10 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39680004
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
 
LVL 7

Author Comment

by:gudii9
ID: 39681328
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
 
LVL 26

Expert Comment

by:dpearson
ID: 39682441
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
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 39683237
How the genaralisation is related to inheritance and 'is a' relationship'

HashMap 'is a' Map .  

Map is a generalised form of HashMap .
0
 
LVL 7

Author Comment

by:gudii9
ID: 39693592
what is 'has a' in this context. please advise
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 26

Expert Comment

by:dpearson
ID: 39694057
"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
 
LVL 7

Author Comment

by:gudii9
ID: 39694534
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
 
LVL 26

Expert Comment

by:dpearson
ID: 39694640
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
 
LVL 7

Author Comment

by:gudii9
ID: 39696343
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
 
LVL 26

Expert Comment

by:dpearson
ID: 39696399
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now