Results 1 to 5 of 5
  1. #1
    mau007 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default I need your advice

    Hi,

    I'm a very young newbie in Java (two weeks !!).
    I would like that you give me your advice concerning my classes definition. I'm sure that some Expert's Java will correct my classes.. :)

    Java Code:
    Class ClassSt
      {
      private String fieldI;
      private String fieldEx;
      private String fieldEn;
      private List <ClassTr> fieldClassStTr = new ArrayList <ClassTr>();
      
      // getter & setter for each attribute
      }
    
    Class ClassTr
      {
      private String fieldOne;
      private String fieldTwo;
      private String fieldThree;
      private String fieldFour;
    
      // getter & setter for each attribute
      }
    
    Class ClassPar extends ClassComp
      {
      private String fieldI;
      private List <ClassComp> fieldClassParComp = new ArrayList <ClassComp>();
      private List <ClassTr> fieldClassParTr = new ArrayList <ClassTr>();
    
      // getter & setter for each attribute
      }
    
    Class ClassComp extends ClassSt
      {
      private String fieldI;
      private String fieldHis;
      private List <ClassTr> fieldClassCompTr = new ArrayList <ClassTr>();
      private List <ClassSt> fieldClassCompSt = new ArrayList <ClassSt>();
    
      // getter & setter for each attribute
      }
    As you can see, I've the same "field" (fieldI) which is present in different classes.
    Another thing : I've some getter & setter which have the same code (like getfieldClassCompTr & getFieldClassStTr,...)

    Maybe an abstract class can help me or maybe an interface ???

    I will appreciate your help.

    Thanks in advance.


    Peregrin

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    mau007 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Hi,

    The problem is the following: I've the same code for differents classes.

    Class ClassSt
    Java Code:
    public String getFieldI()
      {
      return (fieldI);
      }
    
    public void setFieldI(String value)
      {
      fieldI = value;
      }
    Class ClassComp
    Java Code:
    public String getFieldI()
      {
      return (fieldI);
      }
    
    public void setFieldI(String value)
      {
      fieldI = value;
      }
    Class ClassPar
    Java Code:
    public String getFieldI()
      {
      return (fieldI);
      }
    
    public void setFieldI(String value)
      {
      fieldI = value;
      }
    Is it possible to "improve" these classes?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There's a whole lot of extending going on! This might be reasonable or it might not - I'm guessing the latter (on the basis of no information).

    For better guesses you will need to provide more information, like what these classes are supposed to do. It is possible that there's a better way of going about it. Programmers tend to be wary of subclassing. (eg you mentioned replicated code: but code sharing is not a reason to subclass.)

    [Edit] typo. weary too, sometimes, of questions about why foo.getFieldI() returns the value it does...

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    You don't have to override attributes and methods (unless you want to change their behavior or signatures) in extended classes. Just add new attributes or methods...


    Java Code:
    Class ClassComp extends ClassSt{
        private String fieldHis;
        private List <ClassSt> fieldClassCompSt = new ArrayList <ClassSt>();
    
        // getter & setter for NEW attributes
        // and you still have all attributes and methods from ClassSt
    }

Similar Threads

  1. Want an advice
    By baf06 in forum New To Java
    Replies: 3
    Last Post: 04-17-2011, 07:39 AM
  2. got any advice?
    By cejay in forum Networking
    Replies: 2
    Last Post: 03-17-2011, 11:33 AM
  3. looking for OO advice
    By intrepid604 in forum New To Java
    Replies: 7
    Last Post: 02-16-2011, 06:01 PM
  4. need for some advice
    By kasiopi in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2011, 01:36 PM
  5. Im new n looking for an advice
    By azlynn in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 03:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •