Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency
转自:http://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/
These terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff. But still for some, these terms look like Latin and Greek. Just wanted to refresh these terms and explain in simpler terms.
Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.
Example: A Student and a Faculty are having an association.
Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.
Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.
Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don’t go deeper into example and justify relationships!
Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction. It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.
Example: A wire frame model of a car.
Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specializtion to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.
Example: Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization.
Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class. In other words, you can understand this as the relationship between the interface and the implementing class.
Example: A particular model of a car ‘GTB Fiorano’ that implements the blueprint of a car realizes the abstraction.
Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.
Example: Relationship between shape and circle is dependency.
========== 下面是网友的讨论 ==========
Hello,
What is Difference between Association and Dependency ?
U Say ‘ if the contained object cannot exist without the existence of container object, then it is called composition.’–>with example Library–>Books
With your definition ‘Books can not exists without Library’..How’s this ? ..or is it the other way round ‘Library can not exists without Books’??
I am damn confused?
Please please help.Thanks
Association: uses a
Aggregation: has a : strong association
Composition: owns a : weak association
Inheritance: is a
in ooad(Object Oriented Analysis Design and UML)
1)Is association class same as ordinary classs? Explain with example.
plz send ans
“Suppose we have implemented a scenario and we used aggregation in it, after some time we realize that use of aggregation is not a correct choice, now we want to use composition instead of aggregation. Can we replace composition with aggregation? At which phase(s) these changes are required? Justify your answer with solid reasons. ”
sir this question is related by C++
Can u please explain about abstraction and encapsulation oops concepts
Really good article , thanks a lot.
If you can provide the UML examples using symbols(aggregation,composition and generalization) will be more useful.
Hi Joe, very nice explanation.
I just got doubt about the sentence in “Generalisation” , you wrote as – Common structure and behaviour are used from the specializtion to the generalized class.
or it should be as below.
Common structure and behaviour are used from the generalized to the specializtion class.
Correct me on this please.
cool.. :)
Really helpful to brush up the concepts.Thanks for posting such a nice article.
Thanks.:) Clearcut definitions. :)
For association, aggregation and composition could you please put real time exmaples with come classes?
Nice work ..
Indeed this is a very good post.
If i have understood correctly,then will it be ok to say that a child in a mother’s womb is an example of composition and after birth,a mother-child relation can be called aggregation.
Request all learned to please correct me if i have understood incorrectly.
Very helpful and easy t understand
super
Thank you
it was very useful for me
such a nice information .it will help everyone..
Very information. thanks
Its really very easy to understand.. Thanks a lot
simple to understand…with real life egg
Nice explaination
superbly explained.. :)
Very Nice and easy explanation.
Thanks Joe.
good article for basics
nice explanation for UML easy understanding…Please continue this short of topics
earlier i confused to understand about these topic .but by the God i found your site, and understood these all topics with in few moments.
nice site
very good explanation thanks…………..
Hi, Joe!
Thank you for your explanations!
But… you write: “A book cannot exist without a library and therefore its a composition.”
I think more correctly would be as follows:
“A library cannot exist without book and therefore it’s a composition.”
Thankyou
Thank you…..
could you please explain these association,aggregation,composition and dependancies with a sample scenario with java classes implementation…..
This post is very helpfull to understand the basic concept of association and understands the association type aggregation and composition type ….
Dependency explanation is need to be amended as follows.
Dependency is a weaker form of relationship which indicates that one class depends on another because it uses it at some point of time. One class depends on another if the latter is a parameter variable or local variable of a method of the former. This is different from an association, where an attribute of the former is an instance of the latter.
unclear definitions
Had confusion with all these terms..your way of explanation is very simple and straight forward..thanks
It will be great if you have some real life example on this..
Thanks Kausik
bhiya tussi to cha gaye…
I was screwed in a interview for all these.. thanks for sharing…really good one..
thank u …………
very helpful and easy understanding
Content is very good and the site is also looking so good keep it up sir,…
Really a useful stuff to understand object relation ship.
Clear explanation with simplicity!
Thanks
Thanks!!:) It was really worthy.. Short & to the point.. Clarifying examples played vital role in understanding.. ‘Thanks’ once again..
_Js
Sir…it clearly differentiated the terms for a novice programmer.
sir…you should use email validation in comment box…..
and your site content is awesome….i loved it….
Could you please share with java code examples.
A implements B is realization
A extends B is generalization
AM I right?
Nice article.
Adding some example, hope till will support the article.
There are four kinds of Class relationships
Association: uses a
Ex:a Class Man uses a Class Pen
Aggregation: has a
Ex:a Class Man has a Class Car ( Car is still there when Man die )
Composition: owns a
Ex:a Class Man owns a Class Heart ( When Man die, Heart die )
Inheritance: is a
Ex:a Class Man is a Class Human ( Man is a Human )
A relationship between classes of objects
Inheritance>Composition>Aggregation>Association
I Think U are right Hikmat.
In defining the Composition in top of page, I think the sentence should be
“…, if the container object cannot exist without the existence of contained object, then it is called composition”
instead of “…, if the contained object cannot exist without the existence of container object, then it is called composition”
very lucid and helpful. thank you.
ijust hv a doubt about composition ,as a example “A circle is composed of points”.
then i think points are exist without circle ,,but according to u points r not exist without circle ..plz explain.?
I love your blog and find helpful…thanks for your great passion! You people make the world really great!
Nice Article Joe
nice explanation..
Thanks a lot Joe, Very Useful information with simple language and good examples.
Thanks a lot for such an easy explanation. Examples are also easy to understand and correlate.
give me answer plz..
Justifying the statement ,”inheritance is a special case of Generalization”.
Very nice content. I like it. The best site and very comprehensiveness information I ever found.
Nice explanation yaar
Good explanations….
Thanks very much nice information about Generalization, Aggregation, Association, Composition, Dependency,, only one time i have refereed to it but absolutely i got******
good site thanks
Thanks for your inputs
GOOD and NICE and SIMPLE Explanation……..
Great and a very simple way to explain a confusing subject. Thanks a lot for this Post.
nice explanation. very useful. thanks
Good Summary. Thanks a lot!
A simple class diagram to explain the direction of the relationship would be great, if can be added
please explain more sir,with example..
awesome
really helping me a lot….
u r dng a great work.Thank You
Really good man easy to understand
Hi,
Superb Explanation sir .
Nice way of information.
Thanks………………
really clears the confusion.
Wonderful Job Joe. Keep it up.
Good article. One of the questions mentioned above, was related to manager,supervisor and staff. Certainly there is a generalization relationship with Staff as superclass and manager and supervisor classes are subclasses inheriting from Staff. Though attributes could be same in manager and supervisor when it comes behaviour(methods) manager and supervisor classes are specialized with inheriting the generic class i.e., Staff.
@Sanchit
I feel very happy when I get to know that my blog helps a student.
More than anything, I maintain this to help students succeed in their exams / interviews.
Thanks..!! M sure your examples gonna help in tomorrows exam.
Nice one
thank you
identify in detail association and aggregation
Very good explanation
Really simple and excellent explanation. Easy to understand for beginners…!!!
Questions
1. Identify and briefly explain the objectives, classes, method and attributes for a hotel reservation system.
2. Draw a use cases diagram to represent a hotel reservation system.
3. Draw a class diagram that models hotel reservation system data structure, clearly representing the objects, classes, associations, generalization and aggregations.
Thanks
Questions
1. Identify and briefly explain the objectives, classes, method and attributes for a hotel reservation system.
2. Draw a use case diagram to represent a hotel reservation system.
3. Draw a class diagram that models hotel reservation system data structure, clearly representing the objects, classes, associations, generalization and aggregations.
Thanks
Very nice explanation about class diagram terms…
very nice artical!!
A very good explanation..I never seen before like this type explanation about this concept
Thanks you very much for share good knowledge for me .
Hi, may I seek a clarification about generalization concept. Let’s say I have a two kinds of staff, Manager and Supervisor and both have the same attributes (name,contact number). So, is it possible to say that this has a generalization relationship – Staff as the superclass, Manager and Supervisor as its subclasses althought there’s no difference in terms of the attributes between Manager and Supervisor?
Very nice post.. i have never experienced such
simple examples for explaining such complex terms like aggregation and composition
i thought so far.. Keep posting…
You mentioned in this article – Abstraction is specifying the framework and hiding the implementation level information. Abstraction never says about hiding anything. Let’s take an Example of Employee with Super class and Manager and Salesman bening subclass of Employee. So if Employee may provide various implentation in common, but can’t have implementation for calculateBonus because this is fully dependet on nature of employee subclass, and hence Employee would have calculateBonus as abstract. So overall, abstraction is isolation of common essentioal behaviour and supress unimportant behaviours. Abstraction never meanse hiding anything. Hiding of implementation from accidental access is Encapsulation.
Thanks
Arun Deo
Nice explanation on the topics of association, aggregation and composition with quite undrestandable language.
Dear sir, I need more explanation and example about abstraction… How to apply in c…? i use data structure linked list concept..
very good explanation on composition and aggregation relationships
Nice Article, Much useful for a beginner
Superb Article !!!!!!!!!
Nice Article Joe..Like very much the way you describe ,Must say Very good Article for the beginners …!!!
explanation also easy for preparing answerssssssssssss
it is so easy and interesting
thanks for simple explanation of association, aggregation, composition. Good Work…Thank you..
Very Good article… Thank you..
nice explanation.. thiru
Thanks dude.. its really so simple to understand these stuffs…….. really gr8 work.
Useful information with good explanation well done Joseph..
it helped me alot sir………thank u sir
Good Artcile. it helps.. Thanks..!
This article is very helpful and well explained. Thanks
simply super….very useful Thanks for posting..
Very easily explained and easy to understand all the definition. Keep it up
multiplicity is no given here :) pls add
thanks
could you please provide some code samples for association, aggregation and other all oops concepts.
Thanks
Varma
thanks much. very useful information
thankz sir , very nice content
Wow., Awesome work., Thankz,
Keep up the Good work.,
thank you for you very clear work joe.
Very nice article. I am facing a unique situation where we have modeled a test case with two compositions between the same two entities. Is this realistic? If so, can you give me a real world example for this?
Great Work !!!
Keep going on !!!
All the best !!
simple and understanble…
Thanks a lot
it is simple and very easy to understand
thank you,
Nice.. Thank You..
Very nice page.
It contains all required information about association, aggregation, composition.
thanks a lot.
Prabhat.
Thanks for these information. Great definition for aggregation n composition
I was always get confused for these relationship but as you defined these relationship it became easy to understand. Thanks buddy :)
thank you very much for sharing your knowledge. very useful content. I learnt the concept very well to be applied in OOAD.
Hi,
The definition given in this site is very clean and neat and simple and very understandable instead of blah..blah…
Cheers !!!
KM
jst superb…think u r bst faculty for starters..i bliv in it…”with strong foundationz u can aim for the sky.”
i searched most of the definitions for these association,aggregation and composition…u cleared my doubt …u people are explaining like baby feeding thank you……
Terse explanation of Association, Aggregation and Composition..!!! :) Thanks a lot… :)
Looking forward for your next posts…
Good explaination mate
Thank you for addressing all relevant items.. little bit more code would be marvelous. still great. CDW
Thank You Very Much… It Was Very Helpfull…
Thanks Joe for the wonderful post. It is really quite useful.Most of the times we tend to get confused with these terms as all sound the same. Few more additions would make it better:
a) Class diagrams
b) Code for Aggregation and Composition
Sir. i m n trouble..if u post full code .which contain aggregation and compostion.then it will be very clear to all of us.according to programing point of view.
and realy good work u have done.we appriciate ur work…
plz post full code waiting 4 ur reply
Hi ,
thnaks for that information.
but i think i havnet understand that last.(Dependency)
does it comes with arrow or just —- lines. pls post me
Really very good and straight forward description about the Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency. Thank you so much..
Its very handy too..
Cheers,
Prashant.
Thanks Jyotilal for the aggregation example. Actually its otherway around.
A car has a stereo system. A stereo system can exist without a car. There exists aggregation between car and stereo.
Don’t go too technical into electronics and say a car stereo needs a car. Just for an example!
An example for aggregation is missing and it could be like:
A car has a stereo system.A car can exist without a stereo system. There exists aggregation between car and stereo.
very nice content to start
Thanks!
Thank you..
Looking forward for your next post…
Now these basic can be easily differentiated, after read this post
Comments are closed.