1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Designing Java classes

Discussion in 'Java' started by Falguni, Jul 20, 2007.

  1. Falguni

    Falguni New Member

    Joined:
    Jul 20, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jul 20, 2007
  2. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page