Java generic programming with unknown generic type of interface?

Ok, I played a bit with your code and reached a conclusion.

Up vote 1 down vote favorite share g+ share fb share tw.

I'm using several interfaces with generics types. While combining it together I have some problems when I have to use them from a part of the code that is unaware of the concrete type of the generic parameter. Suppose I have the following interface: public interface MyObjectInterface {} The object implementing that interfaceare stored in a generic collection with the same generic type: public interface MyCollectioninterface { public void updateObject(MyObjectInterface o); } Concrete instances of MyCollectionInterface hold several MyObjectInterface of the same generic parameter: public class ConcreteCollection implements MyCollectionInterface { List> list; public void updateObject(MyObjectInterface o){} } Now, I have several questions on how to use these generic interfaces from a client class that is (and must be) unaware of the concrete 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 o){ this.collection.

UpdateObject(o); //this doesn't compile either } public void bar(MyObjectInterface o){ MyObject be = o; //warning this.collection. UpdateObject(o); //this compile but with warnings } } First Question : Considered the fact that ClientClass doesn't care of which concrete type extending Number is the collection, should I declare collection with or without "? "?

If I use the second version I get the following warning: MyCollectionInterface is a raw type. References to generic type LatticeInterface should be parameterized Second Question : Why method foo doesn't compile? Third question: It seems that I need to use bar signature to call updateObject method.

Anyway this solution produce a warning while trying to assign the MyObjectInterface parameter, like in the first question. Can I remove this warning? Last questions: Am I doing something weird with this generic interfaces and I should refactor my code?

Do I really have to care about all these warnings? How can I use safety a generic interface from a class where I don't know its concrete type? Java generics interface raw-types link|improve this question edited Sep 6 '11 at 19:09 asked Sep 4 '11 at 15:17Heisenbug10.6k31036 86% accept rate.

– guardianpt Sep 4 '11 at 15:43 yes, my fault. I fixed it. It's MyObjectInterface.

– Heisenbug Sep 4 '11 at 15:53.

Ok, I played a bit with your code and reached a conclusion. The problem is that your ConcreteCollection (and its interface MyCollectionInterface) declare the method updateObject as receiving an argument of type MyObjectInterface (where T extends Number) - note that the type is a concrete one (not a wildcard). Now, in your client class you are receiving a collection and storing it as MyCollectionInterface but the instance that is passed to ClientClass' constructor will be of a concrete type, for instance: new ClientClass(new ConcreteCollection()); This means that the method updateObject of that instance would only accept an argument of type MyCollectionInterface.

Then, in method foo you are trying to pass a MyObjectInterface to updateObject, but since the compiler doesn't know which generic type your collection accepts (it could be Integer like in my example but it could also be Double or any other type that extends Number), it won't allow any object to be passed. Long story short, if you declare your reference as MyCollectionInterface you won't be able to call updateObject on it. So you have two choices: 1) Pick a concrete type and stick with it: private MyCollectionInterface collection; public ClientClass(MyCollectionInterface collection){ this.

Collection = collection; } public void foo(MyObjectInterface o){ this.collection. UpdateObject(o); //compiles } But then you are limiting the collections you can receive in your constructor (which may not be a bad idea), or: 2) Modify your interface to accept a wildcard type: public interface MyCollectionInterface { public void updateObject(MyObjectInterface o); } public class ConcreteCollection implements MyCollectionInterface { List> list; public void updateObject(MyObjectInterface o) {} } private MyCollectionInterface collection; public ClientClass(MyCollectionInterface collection){ this. Collection = collection; } public void foo(MyObjectInterface o){ this.collection.

UpdateObject(o); //compiles } Also, note that even in 2) you could still run into the same problem in your implementation of the updateObject method unless you declare your list something like this (with ArrayList for example): List> list = new ArrayList>(); In which case you could as well remove the from MyCollectionInterface and ConcreteCollection since T isn't used anymore. @Your last questions: 1) Probably yes 2) You should 3) You can't, if you don't really care which objects you store in the collection, you should ditch generics altogether. Sorry for the long answer, hope this helps.

The problem isn't the method foo. Even if I change it signature the problem is here : public void updateObject(MyObjectInterface o){} . Even if T is declared as T extends Number, the line : this.collection.

UpdateObject(o); doesn't work. I can declare o even and it won't work either. – Heisenbug Sep 6 '11 at 18:56 Edited with a new answer.

– guardianpt Sep 7 '11 at 0:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions