I have 4 classes, all implementing an interface IntSorter
. All classes have a sort
method, so for each class I want to call sort
on an array.
I thought something like this would work:
IntSorter[] classes = new IntSorter[] {
Class1.class, Class2.class, Class3.class, Class4.class
};
for (Class c : classes)
c.sort(array.clone());
But classes
cannot hold the .class names, I get the error Type mismatch: cannot convert from Class<Class1> to IntSorter
So I tried
Class[] classes = new Class[] {
Class1.class, Class2.class, Class3.class, Class4.class
};
for (Class c : classes)
c.sort(array.clone());
But now c.sort() cannot be called. I tried parsing the class to an IntSorter first, with (IntSorter) c
but then I get the error Cannot cast from Class to IntSorter
How do I fix this?