• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Why composition and Aggregation

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I went through the thread https://coderanch.com/t/522414/java/java/Association-Aggregation-Composition

But my Question is Why we have a concept of composition ,aggregation ...In which scenario we should implement it..

I am totally confused about what composition is exactly..Concept? or some process to follow ?

Thanks to explain it...
 
Marshal
Posts: 77158
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to a more appropriate forum. Please don't use coloured text; some people can't read coloured text.

The thre‍ad you quoted has some problems, including that the person who came upon it as an old thre‍ad confused inheritance with composition. Please tell us what you do and what you don't understand.
 
Sarah Jay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Moving to a more appropriate forum. Please don't use coloured text; some people can't read coloured text.

The thre‍ad you quoted has some problems, including that the person who came upon it as an old thre‍ad confused inheritance with composition. Please tell us what you do and what you don't understand.



Ok Campbell..
Let me ask in a different way.
Why do we have a concept called Composition and what is the purpose of composition..Do we have any benefits out of it?
 
Campbell Ritchie
Marshal
Posts: 77158
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's imagine that instead of becoming a programmer, you had decided to become a motor mechanic. Can you see an advantage of considering a vehicle to be made up of parts, e.g. wheels, motor, gearbox? If you buy a 1960s Ferrari without a gearbox, are you going to sue the seller?

If you regard a car as made up of wheels, gearbox, engine, fuel tank, etc., that means you are thinking of composition.
 
Sheriff
Posts: 17405
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the marks of a good program or design is that things are arranged in a logical and orderly way that makes it easy to understand the way different parts of the code relate to each other. This is why we have classes and methods inside classes, because this organization allows us to intuitively understand that the contained methods are “part of” the containing class.

Composition and Aggregation are just ways of adding that kind of intuitive association in your code: you use them to communicate a relationship between two ideas you’re trying to represent in your code.

Be careful about taking real-world relationships and trying to apply them in your program though. Some relationships between ideas represented in your program don’t need to follow all the rules of the real-world things they represent.
 
Sarah Jay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:One of the marks of a good program or design is that things are arranged in a logical and orderly way that makes it easy to understand the way different parts of the code relate to each other. This is why we have classes and methods inside classes, because this organization allows us to intuitively understand that the contained methods are “part of” the containing class.

Composition and Aggregation are just ways of adding that kind of intuitive association in your code: you use them to communicate a relationship between two ideas you’re trying to represent in your code.

Be careful about taking real-world relationships and trying to apply them in your program though. Some relationships between ideas represented in your program don’t need to follow all the rules of the real-world things they represent.



Thanks Junilu...
Very well explaiined ...I can think of it as a best rule to be followed while coding right?
 
Junilu Lacar
Sheriff
Posts: 17405
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Jay wrote:I can think of it as a best rule to be followed while coding right?


I guess you could look at it as a “rule” but I like to think of it more like the Chinese idea of “feng shui” — my code should be arranged in a way that it’s easy to find my way around it, that the ideas the code represents flows naturally into each other, and that changes can be easily made.

Another way of looking at is that you’re trying to tell a story with your program. That means things have to be organized into phrases, sentences, paragraphs, and chapters. Each thing is a part of another thing that is part of a bigger thing and all of it needs to come together in a way that makes sense.
 
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The primary difference between aggregation and composition is that aggregation implies a relationship where the child can exist independently of the parent. On the other hand, composition insinuates a relationship where the child cannot exist independent of the parent.

See this: https://techdifferences.com/difference-between-aggregation-and-composition.html

For me, these are the key points:

The aggregation is a type of association which describes a “has a” type of relationship between the objects. For instance, a car “has a” gearbox, and a car “has an” engine. For the one-to-many relationship, an example is a car “has” many wheels.

The composition describes a “part of” relationship. For example, a leaf is a part of a tree, if the tree is destroyed, then leaves are must be destroyed.


HOPE THIS HELPS.

--mike
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic