<T extends AnInterface> vs <? extends AnInterface>

Viewed 1225

I am a little confused with something.

I have a class where its not a collection, but it does refer to generic objects:

    public class XClass<E extends AnInterface>{

        E instanceobject;

        public void add(E toAdd){}
    }

    public interface AnInterface{}

    public class A implements AnInterface{}

    public class B implements AnInterface{}

I believe I read somewhere that <? extends AnInterface> is to be used (when declaring an instance of XClass) if you want multiple subtype-types in the generic object at the same time, whereas <T extends AnInterface> would only allow you to have a single type of subtype in the generic class at once?

However, I can just use:

    XClass<AnInterface> xc = new XClass<AnInterface>();

    A a = new A();
    B b = new B();

    xc.add(a);
    xc.add(b);

and this way I can pass in multiple subtypes of Supertype to the generic class......

I am not seeing the purpose of using "?" and is there anything wrong with using the Interface as the generic parameter?

4 Answers
Related