Posts

Showing posts with the label super

What is a real life example of generic

I understand that <? super T> represents any super class of T (parent class of T of any level). But I really struggle to imagine any real life example for this generic bound wildcard. I understand what <? super T> means and I have seen this method: public class Collections { public static <T> void copy(List<? super T> dest, List<? extends T> src) { for (int i = 0; i < src.size(); i++) dest.set(i, src.get(i)); } } I am looking for an example of real life use case where this construction can be used and not for an explanation of what it is. The easiest example I can think of is: public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(null); } taken from the same Collections. This way a Dog can implement Comparable<Animal> and if Animal already implements that, Dog does not have to do anything. EDIT for a real example: After some email ping-pongs, I am allowed to present a real e...