Objects and Iterators Can We Have Data Structures With Generic Types? What s in a Bag? All our implementations of collections so far allowed for one data type for the entire collection To accommodate a different data type, we have to modify our implementation Can we have a generic collection that can hold any data type? Can we have a collection that can hold a mix of data types? 2 Widening Conversions Can we do this: public class Foo { } Foo x = new Foo(); Object y; y = x; This is called a widening conversion A widening conversion occurs when you assign an object to a more general (wider) reference variable 3
4 Narrowing Conversions Can we do this: public class Foo { } Foo x = new Foo(); Object y; y = x; Foo z; z = y; Narrowing Conversions No, but we can do this: z = (Foo) y; This is called a narrowing conversion A narrowing conversion occurs when you assign an object to a more specific (narrower) reference variable. We have to use a typecast in this case 5 Generic Collections Can we re-implement our collection classes (bags or sequences using arrays or linked lists) as generic collections that can hold any data type as their data? We can do this by having the data type be Object and using widening and narrowing conversions 6
7 Wrapper Classes With widening and narrowing conversions, we can implement generic collections (collections of Objects) for holding data of any type What about primitive data types? Wrapper classes are defined for primitive data types: Each wrapper class has an instance variable to hold the value Each wrapper class has a one-argument constructor to create a new object with a value Each wrapper class has an accessor method to retrieve the value of the object java allows autoboxing and autounboxing for the values of wrapper classes Standard Wrapper Classes Accessor Limitations of Using Wrappers Primitive operations (e.g. +, -) are no longer directly available Have to use equals for comparison rather than == All wrapper classes implement equals methods that test that their values are equal 9
10 Notes on Using Nodes of Objects Watch out for the use of equals ; overusing it can lead to incorrect code. Only use it when you are comparing data, not when you are trying to find out if 2 objects are really the same object Equals method cannot be used with a null object Notes on Using Nodes of Objects In most cases, when values are retrieved, they must be cast back to their original types When using a clone method, all objects must be cloned as well. This is called deep cloning 11 Generic Methods Generic methods are methods with generic parameters Generic parameter types must be specified in the header of the method preceding the return type Only object types (not basic types) can be substituted for generic types 12
13 Example Generic Method Generic Classes A generic class relies on a generic data type, such as: public class ArrayBag<E> { private E[ ] data; 14 Limitations Only object types can be substituted for the generic types new arrays of the generic type cannot be directly created: solution: create an array of Object and type cast it 15
16 JCL Java Collections Library Java implements several collections classes, such as: Vector and ArrayList: very similar to an array: see Appendix D of the book for implementation Set: similar but enforces that any object can be in the set only once See Java documentation for more collections JCL classes all use objects and can thus be used with any object type Collection Utility Methods binary search: Implemented as a static method of the class Arrays sort: Implemented as a static method of the class Arrays Both methods are overloaded with implementations for the eight primitive types and for Objects 17 Interfaces Interfaces are classes with methods that can be used by any class to simulate multiple inheritance Java provides several built-in interfaces such as: Cloneable (the clone method) Comparable (the compareto method) Users can design their own interfaces by using the keyword interface to declare the interface classes 18
19 Implementing Interfaces To implement an interface, a class must: declare that it implements the interface implement all the methods for the interface A class may implement more than one interface by declaring all of them (separated by commas) Sample interface Declaration 20 Sample Interface Implementation 21
22 Interfaces Whether or not a class implements a particular interface can be tested using the instanceof operator The instanceof operator can also be used to test if an object is an instance of a given class (or any of its super classes) Question to Think About? What is the advantage of using interfaces as opposed to simply implementing the same methods in the different classes? 23 Iterators A Java interface requiring the following methods: public boolean hasnext() public Object next() public void remove() Must import java.util.iterator to use 24
25 Types of Iterators Internal Iterators: Implement the iterator methods as part of the collection class itself External Iterators: Implement the iterator as a separate class with a method in the original class to create an iterator Has the added advantage of having multiple iterators for the same object simultaneously 26 27
28 29 30
31