Java multi Generic collection parameters complie error
Posted
by Geln Yang
on Stack Overflow
See other posts from Stack Overflow
or by Geln Yang
Published on 2010-05-24T03:30:01Z
Indexed on
2010/05/24
3:30 UTC
Read the original article
Hit count: 304
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
© Stack Overflow or respective owner