Avatar of gudii9
gudii9
Flag for United States of America asked on

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
JavaJava EEJSP

Avatar of undefined
Last Comment
dpearson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dpearson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
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
dpearson

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
Sharon Seth

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

HashMap 'is a' Map .  

Map is a generalised form of HashMap .
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
gudii9

ASKER
what is 'has a' in this context. please advise
dpearson

"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
gudii9

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dpearson

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
gudii9

ASKER
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
dpearson

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck