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.
Workera subclass yet.class Factory extends Worker.Factory.load(read)actually callsWorker.load(read)which returns aNoInputWorkerwhich you assign to aFactory... Some pieces of the puzzle are still missing...