Java multi Generic collection parameters complie error
- by Geln Yang
Hi,
So strange!Please have a look the code first:
public class A {
}
public class B extends A {
}
public class C extends A {
}
public class TestMain {
public <T extends A> void test(T a, T b) {
}
public <T extends A> void test(List<T> a, List<T> b) {
}
public static void main(String[] args) {
new TestMain().test(new B(), new C());
new TestMain().test(new ArrayList<B>(), new ArrayList<C>());
}
}
The statement "new TestMain().test(new ArrayList(), new ArrayList())" get a "Bound mismatch" compile error, while "new TestMain().test(new B(), new C())" is compiled ok.
Bound mismatch: The generic method test(T, T) of type TestMain is not applicable for the arguments (ArrayList, ArrayList). The inferred type ArrayList is not a valid substitute for the bounded parameter
It seems the type of the second generic List parameter is limited by the Type of the first.Is it a feature or a bug of the compile program?
ps, jdk:1.6,IDE:Eclipse 3.5.1