1 / 13

Generics

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();

Download Presentation

Generics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generics Fall 2005 OOPD John Anthony

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

More Related