//: generics/Holder.java // ©2015 MindView LLC: see Copyright.txt public class Holder { private T value; public Holder() {} public Holder(T val) { value = val; } public void set(T val) { value = val; } public T get() { return value; } @Override public boolean equals(Object obj) { return value.equals(obj); } public static void main(String[] args) { Holder Apple = new Holder<>(new Apple()); Apple d = Apple.get(); Apple.set(d); // Holder Fruit = Apple; // Cannot upcast Holder fruit = Apple; // OK Fruit p = fruit.get(); d = (Apple)fruit.get(); // Returns 'Object' try { Orange c = (Orange)fruit.get(); // No warning } catch(Exception e) { System.out.println(e); } // fruit.set(new Apple()); // Cannot call set() // fruit.set(new Fruit()); // Cannot call set() System.out.println(fruit.equals(d)); // OK } } /* Output: (Sample) java.lang.ClassCastException: Apple cannot be cast to Orange true *///:~