// Zorah Fung, CSE 143 // Demonstrates usage of the IntList interface. public class InterfaceDemo { public static void main(String[] args) { IntList list1 = new ArrayIntList(); addAndPrint(list1); IntList list2 = new LinkedIntList(); addAndPrint(list2); // Because IntList is an interface, you can't say IntList list3 = new IntList(); // Even though a LinkedIntList has IntQueue methods (like peek), // the list2 variable is of type IntList, so only IntList methods can // be called. list2.remove(); // doesn't compile list2.peek(); // doesn't compile IntQueue queue = new LinkedIntList(); queue.add(72); queue.add(42); queue.remove(); // this does compile! queue.get(0); // this doesn't compile. get(index) is an IntList method // and the IntQueue method doesn't have get(index) } // Calls several methods on an IntList // pre: list is not null public static void addAndPrint(IntList list) { list.add(34); list.add(17); list.add(23); list.add(1, 6); System.out.println(list); list.remove(0); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }