I am very new to Java generics and Class<> clazz type, I have an interface
public interface MyInterface {
void doSomething(String arg1);
}
which is implemented by multiple classes
class MyInterfaceImplFirst implements MyInterface {
private arg;
public MyInterfaceImplFirst(String arg) {
this.arg = arg;
}
@Override
void doSomething(String arg1) {
//...
}
}
class MyInterfaceImplSecond implements MyInterface {
private arg;
public MyInterfaceImplSecond(String arg) {
this.arg = arg;
}
@Override
void doSomething(String arg1) {
//...
}
}
I have a class that accepts Class<MyInterface> type
public class Dummy {
public void load(Class<MyInterface> clazz) {
//...
}
public static void main(String[] args) {
Dummy dummy = new Dummy();
//The below does not compile
dummy.load(MyInterfaceImplFirst.class);
}
}
If I change the load method to public void load(Class clazz) it works fine, can someone tell me how to restrict arguments to load method to be only of subtypes(impl) of MyInterface ?
Aucun commentaire:
Enregistrer un commentaire