I want to manage an ArrayList of classes.
For instance, I have 3 types of filters : TextFilter
, DurationFilter
and TimeFilter
which extend Filter
.
That seems to be correct :
List<Class> filters = new ArrayList<Class>();
filters.add(TextFilter.class);
filters.add(DurationFilter.class);
filters.add(TimeFilter.class);
But now I want to restrain the ArrayList type with
List<Filter.class> filters = new ArrayList<Filter.class>();
filters.add(TextFilter.class);
filters.add(DurationFilter.class);
filters.add(TimeFilter.class);
But it seems that it is not correct. Can you explain why? Do you have a solution to only accept Filter child classes to be adde in my ArrayList? Thanks.
Use a ? in generics.
List<Class<? extends Filter>> filters = new ArrayList<Class<? extends Filter>>();
Since you want class objects and not the instances.
You should be able to say List<Class<? extends Filter>>
See Upper Bounded Wildcards
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