Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
Last active May 28, 2017 19:24
An example of the Java List invariance.
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