This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CSE 12 : Basic data structures and object-oriented design Jacob Whitehill [email protected] Lecture Five 8 Aug 2011 Monday, August 8, 2011 More on interfaces. Monday, August 8, 2011 Review of Iterable and Iterator interfaes • Recall: If a class X is to implement the Iterable interface, then it must implement a method called iterator() which returns an object of type Iterator . • Iterator itself is an interface, not a class; hence, X.iterator() can return an object of any class that implements the Iterator interface. Monday, August 8, 2011 • A class can implement the Iterator interface if it implements the following three methods: // Returns true if the iteration has more // elements. boolean hasNext (); // Returns the next element in the iteration. Object next (); // Removes from the underlying collection the // last element returned by the iterator // ( optional operation ). void remove (); In the case of the List12 interface: “The Iterator returned by iterator() must support the remove() method.” Iterator interface Monday, August 8, 2011 • The user of the Iterator can call these methods whenever he/she wants, subject to the following constraints (as de f ned in the Iterator interface) on remove() : • This method can be called only once per call to next . • remove() should throw an IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method. Iterator interface Monday, August 8, 2011 • The Iterator interface also speci f es that “the behavior of an iterator is unspeci f ed if the underlying collection is modi f ed while the iteration is in progress in any way other than by calling this method.” Iterator interface Monday, August 8, 2011 Iterator interface • The Iterator interface also speci f es that “the behavior of an iterator is unspeci f ed if the underlying collection is modi f ed while the iteration is in progress in any way other than by calling this method.” Modi f cations in the case of DoublyLinkedList12 mean addToFront() , removeFront() , etc. -- anything that changes the contents of the list. Unspeci f ed means that you are “absolved of any responsibility” for maintaining correct functionality in the Iterator if the user modi f es the DoublyLinkedList12 while he/she is iterating over it. Monday, August 8, 2011 Interface as a “contract” • An interface speci f cation serves as a contract between user and implementor of the interface. • The method signatures specify to the user what each method does, and how it is called (i.e., parameters). • The comments describe to the implementor what each method must do and what values to return....
View Full Document
This note was uploaded on 11/02/2011 for the course CSE 12 taught by Professor Gary during the Summer '08 term at UCSD.
- Summer '08
- Data Structures