Search code examples
javagenericstypesmismatch

Type mismatch with generics


Here's an interface:

public interface Foo<T> extends Comparable<Foo<T>>  {
   ...
}

And there are some classes implementing this interface:

public class Bar extends Something implements Foo<Something> {
    public Vector<Foo<Bar>> giveBar() {
        ...
    }
}

public class Boo extends SomethingElse implements Foo<SomethingElse> {
    public Vector<Foo<Boo>> giveBoo() {
        ...
    }
}

Now I want to keep a bunch of Foos (that may really be Foos or Boos) inside a vector.

Bar bar = new Bar();
Boo boo = new Boo();
Vector<Foo<?>> vector;
if (...) 
   vector = bar.giveBar();
else
   vector = boo.giveBoo();

I get:

Type mismatch: cannot convert from Vector<Foo<SomethingElse>> to Vector<Foo<?>>

The same goes for:

Vector<Foo> vector;
if (...) 
   vector = giveBar();
else
   vector = giveBoo();

Is a superclass that both Bar and Boo extend the only solution to this problem?


Solution

  • What all that code boils down to is:

    Vector<A> vector = new Vector<B>();
    

    In this case B extends A, but that's not allowed because the types don't match. To make clear why this doesn't work, imagine the following code:

    Vector<Vector<?>> vector = new Vector<Vector<String>>();
    vector.add(new Vector<Integer>());
    

    The variable's type is of a vector of vectors of unknown type; and what's being assigned to it is a vector of vectors of strings. The second line adds a vector of integers to that. The component type of the variable Vector<?>, which accepts Vector<Integer>; but the actual vector's component type is Vector<String>, which doesn't. If the compiler didn't object to the assignment on the first line, it would allow you to write the incorrect second line without being spotted.

    C#'s generics have a similar restriction, but the difference is that a generic class in C# stores it component type, while Java forgets component types when the code is compiled.

    ps - Why on earth are you using Vector rather than LinkedList or ArrayList? Is it because there are threading issues involved?