130 likes | 243 Views
Generics. Fall 2005 OOPD John Anthony. Remember the Container Problem?. ?. You know the color of the balls as they are placed in the container… …but what color are they when they come out?. For Example…. protected void collectionsExample() { ArrayList list = new ArrayList();
E N D
Generics Fall 2005 OOPD John Anthony
Remember the Container Problem? ? You know the color of the balls as they are placed in the container… …but what color are they when they come out? John J. Anthony
For Example… protected void collectionsExample() { ArrayList list = new ArrayList(); list.add(new String("test string")); list.add(new Integer(9)); inspectCollection(list); } protected void inspectCollection(Collection aCollection) { Iterator i = aCollection.iterator(); while (i.hasNext()) { String element = (String) i.next(); } } John J. Anthony
The Problem We desire a mechanism by which containers can be restricted to specific types. Solution 1: Build a mechanism into the language that allows the developer to specify the type at compile time. Solution 2: Create new classes for each type we desire (e.g. ArrayOfStrings, ArrayOfIntegers, etc.) John J. Anthony
Enter Generics protected void collectionsExample() { ArrayList<String> list = new ArrayList<String>(); list.add(new String("test string")); // list.add(new Integer(9)); this no longer compiles inspectCollection(list); } protected void inspectCollection(Collection<String> aCollection) { Iterator<String> i = aCollection.iterator(); while(i.hasNext()) { String element = i.next(); } } Parameterized Type A class that uses type variables to declare its type at compile time. John J. Anthony
Parameterized Type public class ArrayList<E> extends AbstractList<E> { // details omitted... public void add(E element) { // details omitted } public Iterator<E> iterator() { // details omitted } } //end class Type Variable <E> An unqualified identifier that serves as a placeholder for type identification. John J. Anthony
Some Benefits of Generics • Type Safety • The Collection classes are now type safe without type variation. • Stronger ‘Contract’ • Generics supports stronger interfaces between the client and supplier through type declarations. • Early Binding • Typing errors can be caught at compile time rather than runtime. John J. Anthony
Bounded Genericity You are creating a binary tree data structure (that can be persisted to disk) and you choose to define your class as a parameterized type. public class BinaryTree<E> { // details omitted... public void insert(E element) {…} } //end class How can you guarantee that the type variable ‘E’ will be Comparable and Serializable? John J. Anthony
Solution public class BinaryTree<E extends Serializable & Comparable> { // details omitted... public void insert(E element) {…} } //end class Garanteed that all elements of type ‘E’ will contain the “compareTo(…) method and implement the Serializable interface John J. Anthony
Generic Methods Returns a value whose type is the nearest shared type of the two parameters. Supplier Code public <T extends Comparable> T max(T t1, T t2) { if (t1.compareTo(t2) > 0) return t1; else return t2; } Client Code Ex 1: Integer iresult = max(new Integer(100), new Integer(200)); Ex 2: String sresult = max("AA", "BB"); Ex 3: Number nresult = max(new Integer(100), "AAA"); See any problems? John J. Anthony
Example of Nearest Type Being Returned Compiler Error Example.java:10: incompatible types found : java.lang.Object&java.io.Serializable&java.lang.Comparable<?> required: java.lang.Number Number nresult = max(new Integer(100), "AAA"); John J. Anthony
Inheritance & Overloading Classes can extend generic classes, and provide values for type parameters or add new type parameters by in doing so. For example, all the following are legal: class MyStringList extends ArrayList<String> { ... } class A<X, Y, Z> { ... } class B<M,N> extends A<N, String, Integer> { ... } John J. Anthony
Inheritance & Overloading Generics changes the rules of method overriding slightly. ArrayList: public Object get(int i); MyStringList: public String get(int i); John J. Anthony