• Post Reply Bookmark Topic Watch Topic
  • New Topic

Aggregation in Java  RSS feed

 
Kunder Akshay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Aggregation , a class has an entity reference.
Reading this i thought of a simple example.

Go through the below code and just confirm if i have actually used aggregation or no?



In the above code , i have an Employee class . An Employee HAS-A Address so took a different class for Address.

If the code i have written is not Aggregation , then please help in clearing this concept. Thank you so much team.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a quick look at this article about aggregation and composition. You could say that a Person HAS‑A Head, which is composition. You can also say that removing that Head will mean the Person ceases to be a complete Person. You can say that a Car HAS‑A(n) Engine which is also composition and removing that Engine means the Car ceases to be a complete Car. The difference is that you can add a different Engine object and the Car then becomes a complete Car again, but if you try adding a new Head...
That above is composition, where you can also say the Head is part of the Person and the Engine is part of the Car.
You can't say that an Address is part of an Employee, but you can say that an Employee HAS‑A(n) Address. Changing the Address doesn't make the Employee incomplete or anything, so I think that is aggregation, yes.
You can say that a Society HAS‑(SOME) Members and that is composition. Without Members, the Society ceases to exist, but you can also say that the Members are People, so there are People who HAVE‑A Society. That is association. If a Person doesn't have a Society, they do not become incomplete or anything. There are Societies which have People as their members and there are People who are members of a Society. But there are also People who are not members of a Society and those People are just as normal.
So composition is stronger than aggregation and aggregation is stronger than association.

Another example: You drive somewhere with a friend, and the friend drives back. A Car HAS‑A Driver (you) until you get wherever you are going. You park the Car and then it DOESN'T‑HAVE‑A Driver any more. But it doesn't become at all incomplete. That is normal for parked Cars. Then you go home and the Car again HAS‑A Driver (your friend), somebody different. But it retains the same Engine (composition) throughout.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!