I went through this topics
- http://stackoverflow.com/questions/591004/generics-super-t/591011#591011
- http://stackoverflow.com/questions/2800369/bounding-generics-with-super-keyword
However, I still seem to be kind of lost with super keyword:
When we declare a collection like that:
List<? super Number> list = null; list.add(new Integer(0));//this compiles list.add(new Object());//this doesn't compileshouldn't it be the opposite - we have a list that contains some objects (of unknown type) which are parents of
Number. SoObjectshould fit (since it is the parent ofNumber), andIntegershouldn't. The opposite is the case for some reason.Provided we have the following code
static void test(List<? super Number> param) { param.add(new Integer(2)); } public static void main(String[] args) { ArrayList<String> sList = new ArrayList<String>();//will never compile, however... test(sList); }It is impossible to compile the above code (and my sanity suggests that this is the right behaviour), but the basic logic could prove the opposite:
String is Object, Object is superclass of Number. So String should work.I know this is crazy but isn't this the reason why they didn't allow
<S super T>constructs? If yes, then why<? super T>is allowed?
Could someone help me to restore the missing part of logic chain?