Java extends class as function argument
1 answer
Use an extended constraint pattern:
f(List<? extends A> list)
See Oracle tutorial for details .
Note that this only restricts you to list things in the body of the method; you cannot call consumer methods on a list:
A item = list.get(0); // OK.
list.add(new A()); // Not OK! list might be a List<B>.
+6
source to share