Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 269
user6945925 Avatar asked Sep 03 '26 19:09

user6945925


1 Answers

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.

like image 65
Joe C Avatar answered Sep 06 '26 07:09

Joe C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!