Designing Java classes

Falguni's Avatar, Join Date: Jul 2007
Newbie Member
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?

Last edited by shabbir; 20Jul2007 at 21:26.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
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.