Results 1 to 5 of 5
Thread: I need your advice
- 04-28-2011, 10:06 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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.. :)
As you can see, I've the same "field" (fieldI) which is present in different 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 }
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
-
You haven't told us what your problem is, and we need that information to be able to help you.
- 04-28-2011, 10:18 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Hi,
The problem is the following: I've the same code for differents classes.
Class ClassSt
Class ClassCompJava Code:public String getFieldI() { return (fieldI); } public void setFieldI(String value) { fieldI = value; }
Class ClassParJava Code:public String getFieldI() { return (fieldI); } public void setFieldI(String value) { fieldI = value; }
Is it possible to "improve" these classes?Java Code:public String getFieldI() { return (fieldI); } public void setFieldI(String value) { fieldI = value; }
- 04-28-2011, 10:34 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
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...
- 04-28-2011, 10:57 PM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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
-
Want an advice
By baf06 in forum New To JavaReplies: 3Last Post: 04-17-2011, 06:39 AM -
got any advice?
By cejay in forum NetworkingReplies: 2Last Post: 03-17-2011, 10:33 AM -
looking for OO advice
By intrepid604 in forum New To JavaReplies: 7Last Post: 02-16-2011, 05:01 PM -
need for some advice
By kasiopi in forum AWT / SwingReplies: 3Last Post: 01-26-2011, 12:36 PM -
Im new n looking for an advice
By azlynn in forum New To JavaReplies: 2Last Post: 12-10-2009, 02:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks