Search code examples
javacomposition

Java composition (has-a) relationship clarification


I'm having trouble grasping the concept of composition.

I need to create a manufacturer class and a products class and use composition.

Do I make the has-a reference inside products and just add the manufacturer object when creating a new product?


Solution

  • That is exactly what is intended, and it makes sense. The manufacturer is a perfectly reasonable property of a product, and it seems reasonable to have a reference to the manufacturer in the object.

    About your list, you're not being asked to have a list of products given a manufacturer (at least as far as I can tell). So you don't need the link to go in that direction, and thus not the list you appear to be mentioning.