Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Designing Java classes (http://www.go4expert.com/forums/designing-java-classes-t5363/)

Falguni 20Jul2007 20:02

Designing Java classes
 
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?

kaustubh 15Aug2007 12:39

Re: Designing Java classes
 
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.


All times are GMT +5.5. The time now is 12:10.