Created
March 30, 2018 15:20
-
-
Save Vaysman/1e39ad6905bef57b2fd52e9639b95d51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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