Skip to content

Instantly share code, notes, and snippets.

@anton0xf
Created June 19, 2012 18:09
Show Gist options
  • Save anton0xf/2955644 to your computer and use it in GitHub Desktop.
Save anton0xf/2955644 to your computer and use it in GitHub Desktop.
Java Generics example.
import java.util.*;
class A{ public int i; }
class B extends A { public int j; }
public class TestList{
public static void main(String[] args){
A a = new A(); a.i = 1;
B b = new B(); b.i = 2; b.j = 3;
List<A> alist = new LinkedList<A>();
alist.add(a);
alist.add(b);
System.out.println("a.i: " + alist.get(0).i + "\n"
+ "b.i: " + alist.get(1).i);
// System.out.println("b.j: " + alist.get(1).j);
// COMPILATION ERROR:
// cannot find symbol
// symbol : variable j
System.out.println("b.j: " + ((B)alist.get(1)).j);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment