this code doesn\'t compile. I\'m wondering what I am doing wrong:
private static Importable getRightInstance(String s) throws Exception {
Class
Try:
Class extends Importable> klaz = Class.forName(s).asSubclass(Importable.class);
Here are some snippets to illustrate the problems:
Class klazz = String.class; // doesn't compile!
// "Type mismatch: cannot convert from Class to Class"
However:
Class extends CharSequence> klazz = String.class; // compiles fine!
So for an interface, you definitely need the upper-bounded wildcard. The asSubclass is as suggested by doublep.
Class object to represent a subclass of the class represented by the specified class object. Checks that that the cast is valid, and throws a ClassCastException if it is not. If this method succeeds, it always returns a reference to this class object.