1

Can anyone give code example to show the difference between Aggregation and Composition. I have already Read This and did not understood how they differ in code.

Please show the difference through code.

Community
  • 1
  • 1
Aman
  • 979
  • 3
  • 10
  • 23
  • 3
    Youve got here a nice example in this page. http://stackoverflow.com/questions/11881552/implementation-difference-between-aggregation-and-composition-in-java – carlos gil Jun 23 '15 at 07:31
  • understood that ... thnks fr the link – Aman Jun 23 '15 at 07:36

1 Answers1

0

The main difference between a composition and an aggregation is that a composition is a 1-on-1 relation and aggregation a 1-to-many. To be clear: a class has only one professor and therefore it is a composition relation and a class can have multiple students so it is an aggregation.

To translate this to actual code a Class-object (class in the sense of a class in school) could have a list of students and only a single Professor field. This would indicate that the class has a 1-to-many relationship with students but a 1-on-1 relationship with a professor.

For example in code:

public class SchoolClass
{
    Professor mProfessor;
    List<Student> mStudents;
}
sanderarts
  • 324
  • 2
  • 10