Results 1 to 4 of 4

Thread: General class

  1. #1
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Question 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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 **

  3. #3
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default 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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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

  1. A few general questions
    By Dissonance in forum New To Java
    Replies: 6
    Last Post: 06-30-2012, 12:39 AM
  2. General Error- What does that mean?
    By mathidioticz in forum New To Java
    Replies: 1
    Last Post: 01-22-2012, 01:14 PM
  3. General Java Help
    By ST34LTH in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 03:57 PM
  4. general help.
    By socboy6579 in forum New To Java
    Replies: 1
    Last Post: 10-24-2010, 10:41 PM
  5. general help.
    By socboy6579 in forum New To Java
    Replies: 1
    Last Post: 10-24-2010, 09:20 PM

Posting Permissions

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