Skip to content

Instantly share code, notes, and snippets.

@loganj
Created October 12, 2010 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganj/622702 to your computer and use it in GitHub Desktop.
Save loganj/622702 to your computer and use it in GitHub Desktop.
import java.util.Collections;
import java.util.Set;
class IncompatibleTypes {
interface A {}
interface B<T> {}
static abstract class C<T> {
abstract Set<A> getAsForB(B<T> b);
}
static class B1 implements B<String> {}
static class B2 implements B<Integer> {}
static class C1 extends C<String> {
Set<A> getAsForB(B<String> bstr) {
return Collections.emptySet();
}
}
static class C2 extends C<Integer> {
Set<A> getAsForB(B<Integer> bint) {
return Collections.emptySet();
}
}
public static void main(String[] args) {
C c = new C1();
B b = new B1();
// $ javac IncompatibleTypes.java
// IncompatibleTypes.java:43: incompatible types
// found : java.lang.Object
// required: IncompatibleTypes.A
// for ( A a : c.getAsForB(b) ) {
// ^
// Note: IncompatibleTypes.java uses unchecked or unsafe operations.
// Note: Recompile with -Xlint:unchecked for details.
// 1 error
for ( A a : c.getAsForB(b) ) {
System.out.println(a);
}
}
}
@jschneider
Copy link

No compiler error: Since "c" is delcared as C without type params, the return type of getAsForB is just a plain Set...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment