I\'m trying to make a function that takes one of many classes that extends Foo
, and return a new instance of that object in its Class, not a new instance of F
You can use Java's reflection here.
If you want to get a Class
by just its classname, you use Class.forName
:
Class type = Class.forName('package.SomeClass');
You can check if this class is Foo
or a subclass of it:
boolean isFoo = type.isAssignableFrom(Foo.class);
You can then create a new instance very easily (assuming the constructor takes no arguments):
Object instance = type.newInstance();