0

association, aggregation and composition

I want to get the illustration for above three with simple classes. I read a lot from internet. The conclusion is as-

In aggregation people say-

"Class A contains collection of another Class (say B) and if A is destroyed it will not affect its child that is collection will not be destroyed." How is it possible if one object is destroyed but its property can still exist or what they mean by this.(Am I misinterpreting something)

Class A
{
List<B> lst;
}

Class B
{

}
superman
  • 377
  • 1
  • 5
  • 12
  • There are many questions in so on this. Did you try them – Jayy Feb 23 '12 at 12:54
  • yes checked.....but couldn't get it....so hope to get something in context. – superman Feb 23 '12 at 12:58
  • 1
    possible duplicate of [What is the difference between aggregation, composition and dependency?](http://stackoverflow.com/questions/1644273/what-is-the-difference-between-aggregation-composition-and-dependency) – Don Roby Feb 23 '12 at 13:00
  • possible duplicate of [How to write Composition and Aggregation in java](http://stackoverflow.com/questions/9862286/how-to-write-composition-and-aggregation-in-java) – nawfal Jun 11 '13 at 20:08

1 Answers1

2

Consider the following classes,

class Student
{
    public string Id { get; set; }
    public string Name { get; set; }
}

class Department
{
    public IList<Student> Students { get; set; }

    public void AddStudent(Student student)
    {
        //...
    }

    public void RemoveStudent(Student student)
    {
        //...
    }
}

If you want to add a student to the department, then you call AddStudent() and pass the reference of the Student class instance (note that a reference is passed). So when the department instance is destroyed (set to null for example), then the Students property of that Department instance is no longer available, but the Student instances that have been used to populate this list remain are not destroyed. Hence, the property, in this case a Student instance can still exist.

More information

Community
  • 1
  • 1
Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
  • thanks Devendra!! nice links you provided...looks like I need to explore more from net...BTW...nice explanation. – superman Feb 24 '12 at 04:07