@@ -0,0 +1,2204 @@
+package ch8_general_programming;%0D%0A%0D%0Aimport java.util.*;%0D%0A%0D%0Apublic class item_52_refer_to_objects_by_interface %7B%0D%0A%0D%0A    // We have a method that consumes a Collection%0D%0A    static void consumeCollection(Collection%3CString%3E list) %7B%0D%0A        for(String s : list) %7B%0D%0A            System.out.println(%22...processing : %22 + s);%0D%0A        %7D%0D%0A    %7D%0D%0A    // We have an inferior method that consumes an ArrayList%0D%0A    // Note that this method takes an ArrayList, but doesn't need any implementation specific to an ArrayList%0D%0A    // This method could easily accept any type of Collection to do the same operation%0D%0A    static void consumeCollectionBadly(ArrayList%3CString%3E list) %7B%0D%0A        for(String s : list) %7B%0D%0A            System.out.println(%22...processing : %22 + s);%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    public static void main(String%5B%5D args) %7B%0D%0A        //Lets build a few different types of collections%0D%0A        ArrayList%3CString%3E arrayList = new ArrayList%3CString%3E();%0D%0A        arrayList.add(%22Foo%22);%0D%0A        arrayList.add(%22Bar%22);%0D%0A        arrayList.add(%22Baz%22);%0D%0A        arrayList.add(%22Qux%22);%0D%0A        arrayList.add(1, %22Foo%22);%0D%0A%0D%0A        Stack%3CString%3E stack = new Stack%3CString%3E();%0D%0A        stack.push(%22Foo%22);%0D%0A        stack.push(%22Bar%22);%0D%0A        stack.push(%22Baz%22);%0D%0A        stack.push(%22Qux%22);%0D%0A        stack.push(%22Foo%22);%0D%0A%0D%0A        HashSet%3CString%3E hashSet = new HashSet%3CString%3E();%0D%0A        hashSet.add(%22Foo%22);%0D%0A        hashSet.add(%22Bar%22);%0D%0A        hashSet.add(%22Baz%22);%0D%0A        hashSet.add(%22Qux%22);%0D%0A        hashSet.add(%22Foo%22);%0D%0A%0D%0A        // If we try using our inferior method on each collection, we would have to cast each collection%0D%0A        System.out.println(%22***Processing ArrayList with consumeCollectionBadly%22);%0D%0A        consumeCollectionBadly(arrayList);%0D%0A%0D%0A        // Using our superior method, we can be flexible in how we build collections to give to the method%0D%0A        System.out.println(%22***Processing ArrayList with consumeCollection%22);%0D%0A        consumeCollection(arrayList);%0D%0A        System.out.println(%22***Processing Stack with consumeCollectionBadly%22);%0D%0A        consumeCollection(stack);%0D%0A        System.out.println(%22***Processing HashSet with consumeCollectionBadly%22);%0D%0A        consumeCollection(hashSet);%0D%0A    %7D%0D%0A%7D

Add item_52