Last active
May 28, 2017 19:24
An example of the Java List invariance.
This file contains hidden or 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
import java.util.LinkedList; | |
import java.util.List; | |
interface Powerful {} | |
class Jedi implements Powerful {} | |
class Sith implements Powerful {} | |
public class ListInvariance { | |
public static void main(String[] args) { | |
List<Jedi> jedi = new LinkedList<>(); | |
jedi.add(new Jedi()); | |
List<Powerful> powerfuls = jedi; // Error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment