0

I've been studying PECS in Java for a bit.

I understand that S super T means a type T which is a subclass of S. Isn't that the equivalent of T extends S? Are those two statements interchangable?

If they aren't, why?

//Edit:

I haven't written code with those yet so I'm not 100% on the syntax.

Let's say I have ? super T (I have just seen an example with this). Can't I re-write this as T extends ? ?

Octavian Niculescu
  • 1,177
  • 1
  • 3
  • 24
  • What are you talking about? Can you show some code? There is no such thing as `S super T`. Did you confuse that with `? super T`? – Sweeper Feb 02 '22 at 11:14
  • I thought I can write it as S super T too. I edited my question :) – Octavian Niculescu Feb 02 '22 at 11:18
  • No you can't write `S super T`, nor `T extends ?`. These are syntactically invalid. Also note that things like `T extends S` are constraints for type _parameters_, whereas `? extends T` and `? super S` are type arguments' wildcard bounds. They are completely different things, and used in completely different contexts. – Sweeper Feb 02 '22 at 11:22
  • I think the word "completely" is overstating it there. They're different things, used in different contexts. They aren't *completely* different. – Michael Feb 02 '22 at 11:23
  • No, those constucts aren't interchangeable even if the syntax were valid (and what would `T extends ?` or "T extends anything" mean anyway?). Let's take `Number` instead of `T`: `? extends Number` could resolve to: `Number`, `Integer`, `Double` etc. - `? super Number` could resolve to `Number`, `Serializable` or `Object`. As you can see, those sets are quite different and if you'd invert them you'd get different results, e.g. `? extends Object` would be anything in Java. – Thomas Feb 02 '22 at 11:49

0 Answers0