Question about interfaces and generics in Java: type mismatch error?

Because the Vector is explicitly made up of Interface> getVector() The problem is that for some V implements T or V extends T that Foo{ public Vector> getVector(); } abstract class C implements Interface{ private Vector vector; public Vector> getVector(){ return this. Vector; } }.

Because the Vector is explicitly made up of Interface, not things that extend Interface, I believe this would work if you changed the definition to public Vector getVector(); The problem is that for some V implements T or V extends T that Foo is not a supertype of Foo. The compiler does not test inheritance on the generic arguments unless you explicitly indicate that extension point. Using Vector means "allow any class that implements or extends Interface, whereas Vector means a vector consisting only of Interface items.

Perhaps it's more concrete to consider that List is not an acceptable replacement for List despite Integer extending Number for precisely the same reason. Update: I tested this and the following compiles without any errors or warnings interface Interface{ public Vector getVector(); } abstract class C implements Interface{ private Vector vector; public Vector getVector(){ return this. Vector; } }.

You also have to change the method signature to public Vector> getVector() in subclass C for this to compile. Vector> is not a subclass of Vector>, so returning something that is not a subclass of what the method is supposed to return is obviously not allowed. – toto2 Sep 1 at 23:59 +1: but I edited my question.

Could you help me to clarify that? Is a problem related to generics right? – Heisenbug Sep 2 at 0:03 @toto2: no, it compiles just fine with the modification I suggest (see my edit) – Mark Elliot Sep 2 at 0:04 OK.

I had left Vector> in the method declaration for C. Nonetheless, I think it's clearer to use Vector> as the return type in the declaration. – toto2 Sep 2 at 0:08 ok..now I think to have understood it .

Thanks – Heisenbug Sep 2 at 0:15.

It's the way generics work. They are not "covariant": if you have class AClass and its subclass SubAClass, Vector is not a subclass of Vector. (Note however that SubAClass is a subclass of AClass.) EDIT: The seemingly obvious: public ArrayList blah() { return new ArrayList(); } won't compile because ArrayList is not a subclass ArrayList.

So in your case you can't return a Vector> instance for a Vector>.

– Heisenbug Sep 2 at 0:04 @Overbose Yes, see my edit. – toto2 Sep 2 at 0:10 I don't see how your edit will solve my doubts – Heisenbug Sep 2 at 0:12 2 @toto2: Reification has nothing to do with it. Your examples are correct, but the reason it works the way it does is not because of erasure, but because Java's generics are invariant (and the T extends Foo or T super Foo syntaxes are just a workaround for this) but the feature desired here is covariance.

True, arrays are reified, and arrays are covariant, but the two are completely independent qualities. – Daniel Pryden Sep 2 at 4:29 Thanks @Daniel. I took out my Effective Java by Bloch where covariant and reified are defined at the start of Item 25.

I indeed inverted their meaning. I edited my answer. For reference, arrays being reified means that when you add a String to an array myArray0 = "blah" there is a runtime check that myArray is indeed a String (or a supertype of String).

An ArrayStoreException is thrown if it's not the case.In some way, reification is the opposite model from type erasure used by generics. – toto2 Sep 2 at 13:48.

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