//: holding/ApplesAndOrangesWithGenerics.java import java.util.*; public class ApplesAndOrangesWithGenerics { public static void main(String[] args) { ArrayList apples = new ArrayList<>(); for(int i = 0; i < 3; i++) apples.add(new Apple()); // Compile-time error: // apples.add(new Orange()); for(int i = 0; i < apples.size(); i++) System.out.println(apples.get(i).id()); // Using foreach: for(Apple c : apples) System.out.println(c.id()); } } /* Output: 0 1 2 0 1 2 *///:~