views:

115

answers:

4

In Java type arguments, does mean strictly subtypes only? or would E also suffice?

+7  A: 

It's not strict; E would suffice.

Matt McHenry
I presume the converse is true as well? i.e. <? super E> is not strict and can mean E as well. Thanks!
Aaron F.
Yes, E satisfies <? super E> as well.
Matt McHenry
A: 
List<? extends Animal> animalList=new List<Dog>();
List<? extends Animal> animalList=new List<Animal>();

Both the lines compile without any error. Any function taking the list as a parameter understands that the objects in the list are of type E or a subtype of E.

frictionlesspulley
+2  A: 

Yes, super and extends gives inclusive lower and upper bounds respectively.

Here's a quote from Angelika Langer's Generics FAQ:

What is a bounded wildcard?

A wildcard with an upper bound looks like ? extends Type and stands for the family of all types that are subtypes of Type , type Type being included. Type is called the upper bound.

A wildcard with a lower bound looks like ? super Type and stands for the family of all types that are supertypes of Type , type Type being included. Type is called the lower bound.

polygenelubricants
A: 

E is always a subtype of E, which means that E is a supertype of E, too. (JLS 4.10, Effective Java SE Item 26)

atamanroman