I require your suggestion in designing a few class structures. The below mentioned are the 2 class hierarchies. Code: class ProductBase { //common attributes } class Product1 extends ProductBase { } class Product2 extends ProductBase { } class X { //common attributes. } class ProductY extends X { String y; } Class ProductZ extends X { String z; } I want to use class ProductY and ProductZ in class Product1 and Product2 as attributes respectively i.e. ProductY is specific Product1 and ProductZ is specific to Product2. What would be an ideal way to design the class structures? Method 1 Code: class ProductBase { //common attributes X obj = null; } class Product1 extends ProductBase { //constructor public Product1() { super.obj = new ProductY(); } } class Product2 extends ProductBase { //constructor public Product2() { super.obj = new ProductZ(); } } Method 2 Code: class ProductBase { //common attributes } class Product1 extends ProductBase { ProductY obj = new ProductY() } class Product2 extends ProductBase { ProductZ obj = new ProductZ(); } In this case does it matter the way I define the attributes?
hello falguni , you should implement in your program and see trying the both methods. You can read examples online to see which method is used more.