Java programming with an unknown common interface type

I use several interfaces with generic types. Combining it together, I have some problems when I have to use them from a piece of code that does not know a specific type of general parameter.

Suppose I have the following interface:

public interface MyObjectInterface<T extends Number> {}

An object that implements this interface is stored in a common collection with the same general type:

public interface MyCollectioninterface<T extends Number> {
    public void updateObject(MyObjectInterface<T> o);
}

Specific instances of MyCollectionInterface contain multiple MyObjectInterface objects of the same common parameter:

public class ConcreteCollection<T extends Number> implements
 MyCollectionInterface<T> {
    List<MyObjectInterface<T>> list;
    public void updateObject(MyObjectInterface<T> o){}

}

Now I have a few questions about how to use these common interfaces from a client class, which (and should be) do not know the specific type of generics.

Suppose I have the following class:

public class ClientClass{

    private MyCollectionInterface<?> collection;  //1st possibility
    private MyCollectionInterface collection;  //2nd possibility

    public ClientClass(MyCollectionInterface<?> collection){
        this.collection = collection;
    }

    public void foo(MyObjectInterface<?> o){
         this.collection.updateObject(o);  //this doesn't compile
    }
    public void foo(MyObjectInterface<? extends Number> o){
         this.collection.updateObject(o);  //this doesn't compile either
    }
    public void bar(MyObjectInterface o){
         MyObject b = o; //warning
         this.collection.updateObject(o);  //this compile but with warnings
    }
}

First question :

  • , ClientClass , Number , ??? , :

MyCollectionInterface . LatticeInterface

:

  • foo ?

:

  • , updateObject. MyObjectInterface, . ?

:

  • - , ?
  • ?
  • , ?
+2
1

, .

, ConcreteCollection ( MyCollectionInterface) updateObject MyObjectInterface<T> ( T extends Number) - , ( ).

, MyCollectionInterface<?> , , ClientClass ", , :

new ClientClass(new ConcreteCollection<Integer>());

, updateObject MyCollectionInterface<Integer>.

foo MyObjectInterface<?> updateObject, , ( Integer , Double , Number), - .

, MyCollectionInterface<?>, updateObject. , :

1) :

private MyCollectionInterface<Number> collection;

public ClientClass(MyCollectionInterface<Number> collection){
    this.collection = collection;
}

public void foo(MyObjectInterface<Number> o){
     this.collection.updateObject(o);  //compiles
}

, ( ), :

2) , :

public interface MyCollectionInterface<T extends Number> {
    public void updateObject(MyObjectInterface<? extends Number> o);
}

public class ConcreteCollection<T extends Number> implements MyCollectionInterface<T> {
    List<MyObjectInterface<T>> list;
    public void updateObject(MyObjectInterface<? extends Number> o) {}
}

private MyCollectionInterface<?> collection;

public ClientClass(MyCollectionInterface<?> collection){
    this.collection = collection;
}

public void foo(MyObjectInterface<?> o){
     this.collection.updateObject(o);  //compiles
}

, 2) updateObject, list - ( ArrayList ):

List<MyObjectInterface<? extends Number>> list = new ArrayList<MyObjectInterface<? extends Number>>();

<T extends Number> MyCollectionInterface ConcreteCollection, T .

@ :

1) , 2) 3) , , , .

, , .

+1

All Articles