I have this method with a generic class as parameter:
myMethod(Class myclass){
Superclass superclass = myclass;
}
then I use the method by passing a child class of the Superclass
myMethod(Mychildclass.class)
Netbeans is giving me a warning that I am using generic "Class". However this works fine. If I change the method's parameter to this more specific
myMethod(Class<Superclass> myclass){
this.superclass = myclass;
}
then I am getting an error when trying to use my method:
incompatibles types: Class<Mychildclass> cannot be converted to Class<Superclass>
So my question: Why is this not working? How can I make Netbeans happy giving me no warning and no error messages?
Try this instead:
private Class<? extends Superclass> superclass;
void myMethod(Class<? extends Superclass> myclass){
this.superclass = myclass;
}
If you use Class<Superclass>
,
only Superclass.class
is expected.
If you use Class<? extends Superclass>
,Superclass.class
or any of its child classes are expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With