Skip to content

Instantly share code, notes, and snippets.

@Vaysman
Created March 30, 2018 15:20
Show Gist options
  • Save Vaysman/1e39ad6905bef57b2fd52e9639b95d51 to your computer and use it in GitHub Desktop.
Save Vaysman/1e39ad6905bef57b2fd52e9639b95d51 to your computer and use it in GitHub Desktop.
public class Lists {
public static void main(String[] args) {
// Lower Bounded Wildcards
m1(new ArrayList<>()); // <> = C
m1(new ArrayList<B>());
m1(new ArrayList<D>()); // Error
// Upper Bounded Wildcards
m2(new ArrayList<>()); // <> = C
m2(new ArrayList<D>());
m2(new ArrayList<B>()); // Error
}
private static void m1(List<? super C> l) {
l.add(new C());
}
private static void m2(List<? extends C> l) {
System.out.println(l.get(1));
}
}
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment