Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
Last active May 28, 2017 19:24
Show Gist options
  • Save sw-samuraj/7f13a08bc83eb20bbe655b0fb738b4dd to your computer and use it in GitHub Desktop.
Save sw-samuraj/7f13a08bc83eb20bbe655b0fb738b4dd to your computer and use it in GitHub Desktop.
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