Results 1 to 10 of 10
  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default How use abstract class with 2 subclasses with serialzable and comparator interface

    For example, I have abstract class worker with fixed and hourly salary.
    I need to create the collection of this two types.
    I can do it by
    Java Code:
     Arraylist workers=…
    …add(new HourlyWorker(…)
    …add(new FixedWorker(…)
    as well as Worker 1= new HourlyWorker(…);
                     Worker 2= new FixedWorker (…);
    And add(1);
            add(2);
    to save the base class reference.


    But if I want to compare its list of workers by salary how to do it.
    Java Code:
     If I use for(int i=0; i<workers.size();i++)
    {System.out.print(Worker).workers.get(i);)
    I just got the abstract base class but not one of subtype, so how to do it correctly, if possible in principle, or I should use if
    Java Code:
     (if (workers.get(i) instanceof (HourlyWorker)) 
    { System.out.print((HourlyWorker).workers.get(i)) }
    else …
    anyway can I use
    Java Code:
     abstract class Worker implement Serializable, Comparator<Worker>
    with int compareTo() overridden?
    In case of serialization I need to write to collection the list of this 2 subclasses, then
    To read – so readObject() I need to also cast to FixedWorker or HourlyWorker, or just to (Worker)
    So 2 questions is it possible to implement this abstract class to serialization and comparation, and how to cast to 2 distinct sub-classes (or maybe to implement everything in one class, that should be possible, but I have the task to use classes so it should be some kind of extension—maybe base class could be fixed but subclass—hourly—but then there should be the issue of casting to one of classes)?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    compare its list of workers by salary
    What if the salary were in the base class?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    Untill now I left the salary field empty (just 0) or even absent in base abstract class - that is defined just in subclass by setSalary - as there is two formulas -- constant and hourly dependent. Anyway how to create compareTo method with 2 different despite similar class with common ancestor. How to write implement Serialzable differently?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    I left the salary field empty
    Why? If a Worker has a salary, that's where it goes.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    No Worker is abstract class so I could not instantiate it. Anyway I not confined by anstract class but I need have "classes" two and more - so I need to compareTo() overriding for two classes at least - so should it be right - Worker implement Comparator<Worker> and use such statement as Worker ww=new HourlyWorker(....) that ww is not just reference but comparable item as well.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    If you have code you need help with, paste it here and be sure to wrap it in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    So I want to return to question: if i have class fixedworker extends worker. And the generic collection ... List <worker> workes = new ArrayList<worker> (); can I add object of subclass in such way directly: workes.add(new FixedWorker(John, 1980, 5)? As I am learned we can have just abstract base class reference and assign it the object of subclass. Could such add(new ...) to be compatible with such schema when parameter is abstract base class Worker? The kind of probable assigned reference.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    can I add object of subclass in such way directly:
    Did you try it? What happened?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    I want to have theoretical answer for now.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How use abstract class with 2 subclasses with serialzable and comparator interfac

    Why not write ask the compiler by writing the code to see what it says. Then you would know if it would work. If you get errors, post the code and the error messages.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 26
    Last Post: 08-22-2015, 07:29 PM
  2. interface and abstract class
    By sid123 in forum New To Java
    Replies: 7
    Last Post: 04-24-2013, 04:19 PM
  3. When to use Abstract class and Interface?
    By vivekjan17 in forum New To Java
    Replies: 1
    Last Post: 03-05-2012, 11:37 AM
  4. Interface and abstract class
    By vivekbarange in forum New To Java
    Replies: 8
    Last Post: 07-12-2011, 02:22 PM
  5. Replies: 6
    Last Post: 02-11-2009, 11:51 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
  •