3

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?

  • (composition) has-a example http://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition – Nick Bell Oct 09 '16 at 19:12
  • `I also have to make a products arraylist inside the class as well to hold them all` This sounds right to me. – Zarwan Oct 09 '16 at 19:14
  • As far as I understand this correctly, the Product class should have a static List where all the new Product objects are stored and the Product objects should hold a field for its manufacturer – Mad Matts Oct 09 '16 at 19:37

1 Answers1

2

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.

Joe C
  • 15,324
  • 8
  • 38
  • 50