0

Why does the following work:

public void test(Class<? super Integer> c) { ... }

But the following doesn't:

public <T super Integer> void test(T c) { ... }

Is there any substantial difference I am missing? Using extends works just as expected.

Dimitrios Begnis
  • 823
  • 6
  • 16
  • public void test(T c) { ... } will work. – Vishal Gajera Mar 31 '16 at 07:57
  • If you have `test(T)` where `T` is allowed to be superclass of `Integer`, then `test()` accepts *any object*. The generic parameter becomes pointless. – khelwood Mar 31 '16 at 07:57
  • @khelwoodh why does the first version work though? Also, Integer extends Number and Object, but that doesn't mean any subclass of Object can be passed in - is that right? – Dimitrios Begnis Mar 31 '16 at 08:00
  • @khelwood For example, the first version doesn't accept String.class – Dimitrios Begnis Mar 31 '16 at 08:01
  • @Dimi `Object` is a superclass of `Integer`. So `T` is allowed to be `Object`. If you have a method `test(Object c)` then _any object_ can be passed to `test()`. Any instance of a subclass of `Object` is also an instance of `Object`. – khelwood Mar 31 '16 at 08:04

0 Answers0