2

I am trying to create a new Instance of a subclass from my Super Class. This is my super class

public abstract class Worker {

    String world;

    protected abstract void onLoad(Scanner read);

    public static Worker load(Scanner read) {
        // I want to create the instance of my sub class here and call it w
        w.onLoad(read);
        return w;
    } 

    public void setWorld(String world) {
        this.world = world;
    }

}

And this is my subclass

public class Factory extends Worker {

    @Override
    protected onLoad(Scanner read) {
        setWorld(read.readline());
    }

}

And this is what I want to do with those classes.

public class MainClass{

    public List<Factory> loadFactories() {
        List<Factory> facts = new ArrayList<Factory>();
        Scanner read = new Scanner(new FileInputStream("factory.txt"));

        while(read.hasNextLine()) {
            Factory f = (Factory)Factory.load(read);
            facts.add(f);
        }

        read.close();
        return facts;
    }

}

Is there any way I can do this without starting over? Thanks for any help.

9
  • You haven't made Worker a subclass yet. Commented Nov 11, 2012 at 21:22
  • 2
    A good start if you want Factory to be a subclass of Worker is to write: class Factory extends Worker. Commented Nov 11, 2012 at 21:22
  • 2
    What's supposed to be a subclass of what here? Commented Nov 11, 2012 at 21:24
  • Sorry I wrote this really fast. Commented Nov 11, 2012 at 21:29
  • 1
    @user1816686 It is difficult to understand what you are trying to achieve. Factory.load(read) actually calls Worker.load(read) which returns a NoInputWorker which you assign to a Factory... Some pieces of the puzzle are still missing... Commented Nov 11, 2012 at 21:33

1 Answer 1

2

Is this what you want?

public static Worker load(Scanner read) {
    Factory w=new Factory();
    w.onLoad(read);
    return w;
} 

Edit:

public class MainClass {

    public List<Factory> loadFactories() throws FileNotFoundException, InstantiationException, IllegalAccessException {
        final List<Factory> facts = new ArrayList<Factory>();
        final Scanner read = new Scanner(new FileInputStream("factory.txt"));

        while (read.hasNextLine()) {
            final Factory f = Worker.load(read, Factory.class);
            facts.add(f);
            final Pipeline p = Worker.load(read, Pipeline.class);
        }

        read.close();
        return facts;
    }

    static public class Factory extends Worker {

        @Override
        protected void onLoad(final Scanner read) {

        }

    }

    static public class Pipeline extends Worker {

        @Override
        protected void onLoad(final Scanner read) {

        }

    }

    static public abstract class Worker {

        String world;

        protected abstract void onLoad(Scanner read);

        public static <T extends Worker> T load(final Scanner read, final Class<T> t) throws InstantiationException, IllegalAccessException {
            final T w = t.newInstance();
            w.onLoad(read);
            return w;
        }

        public void setWorld(final String world) {
            this.world = world;
        }

    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Not sure why this has been down voted- it's the only answer if you take the question at face value!
To me it look like a perfect example of Factory design pattern! I cannot possibly interpret it in another way. Of course the main method should be Worker f = Worker.load(read);
Yes but not exactly. Lets say I have another Worker class called Pipeline and I want to tell if it is a Pipeline or a Factory (without an enum).
like if (w instanceof Pipeline)... ?
Factory f = (Factory)Worker.load(read); could lead to problems - if you add the Pipeline class. A typical use is to return Worker, without letting the user know what class you are ACTUALLY passing back (allowing you to change it later)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.