Search code examples
javadatabase-designarchitectureentity

Object Oriented design - how to nest one class into another class


I come from the world of relational databases and I find myself having to create my first object oriented program, and despite studying, I just cannot grasp how to do the most basic things.

I have defined a Product class (int id, String productName, double price...), with its methods and attributes, and a ProductFamily class (String familyName, String costCenter...) with its own methods and attributes.

How should link/embed both classes? I've tried using subclasses with the "extends" keyword, but this just seems to inherit the methods from one to another. It doesn't let me instantiate a new product object into a given product family.

Thank you!!


Solution

  • You have to add a field to your Product class, which is an object to a ProductFamily class. By adding a method getProductFamily() in your Product class you can return the product familys for the specific product.