Say I have two classes Foo and Bar which implement interface A. I either want to make an ArrayList of Foo or Bar. In the command line arguments, the user must input either Foo or Bar as to which object they are trying to make an ArrayList of.
So:
public static void main(String [] args){
String type = args[0];
ArrayList<type> list = new ArrayList<type>();
}
How do I go from the string type to a reference to the class Foo or Bar so it's doing
ArrayList<Foo> list = new ArrayList<Foo>();
or
ArrayList<Bar> list = new ArrayList<Bar>();
This way I can do the following:
for(int i = 0 ; i < list.size() ; i++){
list(i).doSomething();
//doSomething is a function in interface A that Foo and Bar are required to implement
}
To be clear, I don't want something like
if (type.equals("Foo")) list = ArrayList<Foo>();
I want to be able to make an ArrayList with any valid class with the same name as type.