Question

I have one method with a parameter Class<SearchForm> formType, and in the method I call formType.newInstance() to get an instance of the form. I need to be able to pass in child classes of SearchForm, but when I try to pass in MySearchForm.class (where MySearchForm extends SearchForm), eclipse tells me I cannot pass in Class<MySearchForm> for a parameter of type Class<SearchForm>.

How can I pass in a class extending the desired parent class and then instantiate an object of that class?

Was it helpful?

Solution

You could use an upper bound generic wildcard Class<? extends SearchForm>

OTHER TIPS

You can't pass in a Class<MySearchForm>, because it's not a Class<SearchForm>, even if a MySearchForm is a SearchForm.

However, you can use a wildcard parameter in your method, defined something like this:

... yourMethod(Class<? extends SearchForm> formType)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top