Monday, June 22, 2009

What is association, aggregation and composition in oops

Association
Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.
Points:
• Is a Relationship between objects.
• Objects have independent lifecycles.
• There is no owner.
• Objects can create and delete independently.

Aggregation
Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Points:
• Specialize form of Association.
• has-a relationship between objects
• Object have independent life-cycles
• Parent-Child relationship
Composition
Composition is again specialize form of Aggregation. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete.
Points:
• Specialize form of Aggregation
• Strong Type of Aggregation.
• Parent-Child relationship
• Only parent object has independent life-cycle.

26 comments:

  1. The perfect explanation I have ever found from the internet searches regarding OOP relationships. I understood all very easily and clearly.

    Thank you very much.
    Chandima.

    ReplyDelete
  2. I too agree, a perfect explanation that I was looking for.


    Thank you,
    Gaurav.

    ReplyDelete
  3. Just superb.. no need to search more.....
    thanx dear

    ReplyDelete
  4. explain about dependency also

    ReplyDelete
  5. What is a 'is - a' relationship

    ReplyDelete
    Replies
    1. 'is-a' relation is something like Generalization specialization

      Employee is a Manager kind of

      Delete
  6. I too agree it is perfect explanation

    ReplyDelete
  7. aggregarion has lower or higher level entity

    ReplyDelete
  8. Hi

    Tks very much for post:

    I like it and hope that you continue posting.

    Let me show other source that may be good for community.

    Source: Science teacher interview questions

    Best rgs
    David

    ReplyDelete
  9. thank you very much. this is the exact answer that we want!!

    ReplyDelete
  10. hatts of to you Sir... Very much satisfactory explanation .... I usually do not comment on blogs... but this forced me to do ... once again thnx...

    ReplyDelete
  11. Thank you for that easy and perfect example..

    ReplyDelete
  12. Hi, In association contains IS-A relation ship.if IS-A relation ship is there then owner also there.I dont know whether association you have mentioned four points right or wrong.

    ReplyDelete
  13. The best explanation in whole universe good job son

    ReplyDelete
  14. Very, very good explanation!!! I searched in various site about oop, but not have an explanation clear... In this article, described points for check before of choice the association type.

    very good! Simple but useful.

    ReplyDelete
  15. in association you write in 1st point
    Points:
    • Is a Relationship between objects.

    while in the wikipedia it's defined as >>relationship between classes
    check >> http://en.wikipedia.org/wiki/Association_(object-oriented_programming)
    in the 1st line
    if you have replay please send it to amrosama@gmail.com

    ReplyDelete
  16. really it is simple explaination...thanks..............

    ReplyDelete
  17. excellent examples and good explanation

    ReplyDelete
  18. Really simple yet easy to understand explanation. Except one , where the example if Question and Option is given.

    AS, if we think in broad way, we can have sometime an options that may belong to more than one questions ( just to create further confusion :) )

    but, really nice explanation

    ReplyDelete
  19. Nice Explaination, but comparing in this "http://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language" link they mismatched Aggregation and Composition refer second answer.

    ReplyDelete
  20. Is a relationship shows inheritance.

    ReplyDelete

 
Locations of visitors to this page