Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Generics Fall 2005 OOPD John Anthony. 2John J. Anthony Object Oriented Programming & Design Remember the Container Problem? ? You know the color of.

Similar presentations


Presentation on theme: "1 Generics Fall 2005 OOPD John Anthony. 2John J. Anthony Object Oriented Programming & Design Remember the Container Problem? ? You know the color of."— Presentation transcript:

1 1 Generics Fall 2005 OOPD John Anthony

2 2John J. Anthony Object Oriented Programming & Design 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?

3 3John J. Anthony Object Oriented Programming & Design For Example… protected void inspectCollection(Collection aCollection) { Iterator i = aCollection.iterator(); while (i.hasNext()) { String element = (String) i.next(); } protected void collectionsExample() { ArrayList list = new ArrayList(); list.add(new String("test string")); list.add(new Integer(9)); inspectCollection(list); }

4 4John J. Anthony Object Oriented Programming & Design 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.)

5 5John J. Anthony Object Oriented Programming & Design Enter Generics protected void collectionsExample() { ArrayList list = new ArrayList (); list.add(new String("test string")); // list.add(new Integer(9)); this no longer compiles inspectCollection(list); } protected void inspectCollection(Collection aCollection) { Iterator 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.

6 6John J. Anthony Object Oriented Programming & Design Parameterized Type public class ArrayList extends AbstractList { // details omitted... public void add(E element) { // details omitted } public Iterator iterator() { // details omitted } } //end class Type Variable An unqualified identifier that serves as a placeholder for type identification.

7 7John J. Anthony Object Oriented Programming & Design Some Benefits of Generics Type Safety Type Safety The Collection classes are now type safe without type variation. The Collection classes are now type safe without type variation. Stronger ‘Contract’ Stronger ‘Contract’ Generics supports stronger interfaces between the client and supplier through type declarations. Generics supports stronger interfaces between the client and supplier through type declarations. Early Binding Early Binding Typing errors can be caught at compile time rather than runtime. Typing errors can be caught at compile time rather than runtime.

8 8John J. Anthony Object Oriented Programming & Design 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 { // details omitted... public void insert(E element) {…} } //end class How can you guarantee that the type variable ‘E’ will be Comparable and Serializable?

9 9John J. Anthony Object Oriented Programming & Design Solution public class BinaryTree { // 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

10 10John J. Anthony Object Oriented Programming & Design Generic Methods public T max(T t1, T t2) { if (t1.compareTo(t2) > 0) return t1; else return t2; } Client Code Supplier 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"); Returns a value whose type is the nearest shared type of the two parameters. See any problems?

11 11John J. Anthony Object Oriented Programming & Design Example of Nearest Type Being Returned 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"); Compiler Error

12 12John J. Anthony Object Oriented Programming & Design 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 {... } class A {... } class B extends A {... }

13 13John J. Anthony Object Oriented Programming & Design Inheritance & Overloading Generics changes the rules of method overriding slightly. ArrayList: public Object get(int i); MyStringList: public String get(int i);


Download ppt "1 Generics Fall 2005 OOPD John Anthony. 2John J. Anthony Object Oriented Programming & Design Remember the Container Problem? ? You know the color of."

Similar presentations


Ads by Google