<? extends Type>
is a bounding wildcard generic. A collection defined in this way could be of any subclass of type, or Type
. ie.
Collection<Type> typeCollection;
//or
Collection<SubType> subtypeCollection;
//where subtype is...
class SubType extends Type
All that matters in this case is that ?
is of type Type
.
Collection<Type>
must be return a collection of Type
. ie.
Collection<Type> collection;
Read the tutorials here. for more information. Which you chose will depend on your needs.
Here's an example. I use bounded wildcards when defining renderable item groups. For example.
public class SpriteGroup<T extends Sprite>
This would be a collection for Sprites, or any subclass of Sprite. This is useful because then I can define groups like so:
SpriteGroup<PhysicalSprite> physicalSprites = new SpriteGroup<PhysicalSprite>();
PhysicalSprite physicalSprite = physicalSprites.get(0);
SpriteGroup<ParticleSprite> particleSprite = new SpriteGroup<ParticleSprite>();
ParticleSprite particle = particleSprite.get(0);
Any get/set routines then return the type I specified (PhysicalSprite, ParticleSprite), which is desirable.
If I'd defined it as:
SpriteGroup<Sprite> spriteGroup = new SpriteGroup();
//all I can retrieve is a sprite, gotta cast now...
Sprite sprite = spriteGroup.get(0);
I'd need to cast them to access properties specific to each type of Sprite. Any subclass of SpriteGroup
would be restricted likewise.
Collection<Type>
is not the same asCollection<SubType>
where SubType extends Type. – Pilfer