0

I'm learning Java generics type and I saw some syntax like <? super T>, <? extends T> and <T extends SomeClass>.

If:

public class MyClass<T extends Number> {}

means MyClass can be constructed only with a generic type that is the subclass of Number,

what would be the correct syntax for declaring a class that only accepts Number and its superclass as its generic type, like:

//public class MyClass<T super Number> {}
yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Please see the linked question and its answers for why this does not make sense. – Miserable Variable Jan 13 '14 at 21:01
  • @MiserableVariable Actually I'm critical of that accepted answer and become sad whenever more people upvote it. But yes, lower bounds don't make sense on *class*-level type parameters. – Paul Bellora Jan 13 '14 at 21:20
  • @PaulBellora can elaborate why you don't like that answer? I found the explanation under non-array type adequate. Can you also describe where lower bounds may make sense? – Miserable Variable Jan 13 '14 at 22:40
  • @MiserableVariable Sorry I didn't quite get that... According to the best answer of that question, he is trying to prove that creating a method that only accepts Integer, Number, and Object is impossible... But I do feel it is kind of possible if you put it in any Collection types.. like "public void method(ArrayList super Integer> arg){}", this method actually does accept only ArrayList, ArrayList, and ArrayList, even if String is Object... – hugo00618 Jan 14 '14 at 00:29
  • @MiserableVariable See the comments under that answer and [Rotsor's counterexample answer](http://stackoverflow.com/a/5671079/697449). – Paul Bellora Jan 14 '14 at 01:09

0 Answers0