Results 1 to 4 of 4
Thread: General class
- 11-20-2012, 11:09 PM #1
Member
- Join Date
- Aug 2010
- Location
- Leuven, Belgium
- Posts
- 86
- Rep Power
- 0
General class
Hi,
I have several small classes(Service, client, device, ...)
Each with their own set of attributes and implemeting the same interface.
I want to create for each class an arrayList and serialize this list (Service.ser, client.ser, device.ser, ....).
I can create an class that takes an ArrayList<interface> and include the 4 major functions (save, read, add and search).
but then i can add a service to a device arraylist because both implements the interface
what is the right way of doing ?
Kind regards
Dipke
- 11-21-2012, 10:42 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: General class
So have specific Lists then.
The only reason to create a List<interface> is if you are happy adding any <interface> object to it.
If you want to ensure a List only has <Service> then it needs to be a List<Service>.
The code should reflect the logic, including what types are allowed in your collection.Please do not ask for code as refusal often offends.
** This space for rent **
- 11-21-2012, 01:13 PM #3
Member
- Join Date
- Aug 2010
- Location
- Leuven, Belgium
- Posts
- 86
- Rep Power
- 0
Re: General class
Hi,
So that means that instead of one general class, i have to create for each list a separate class with the list in it and implement the 4 functions (save, read, search and add).
Ex :
class Service with the attributes, getters, setters, equals, .....
class ServiceList with ArrayList<Service> and the 4 functions
kind regards
- 11-21-2012, 02:17 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: General class
Sounds like a use for generics.
If you haven't done those yet though then it would be separate classes.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
A few general questions
By Dissonance in forum New To JavaReplies: 6Last Post: 06-30-2012, 01:39 AM -
General Error- What does that mean?
By mathidioticz in forum New To JavaReplies: 1Last Post: 01-22-2012, 02:14 PM -
General Java Help
By ST34LTH in forum New To JavaReplies: 2Last Post: 11-07-2010, 04:57 PM -
general help.
By socboy6579 in forum New To JavaReplies: 1Last Post: 10-24-2010, 11:41 PM -
general help.
By socboy6579 in forum New To JavaReplies: 1Last Post: 10-24-2010, 10:20 PM
Bookmarks