Created
June 19, 2012 18:09
-
-
Save anton0xf/2955644 to your computer and use it in GitHub Desktop.
Java Generics example.
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
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