Unformatted text preview:

Chapter 9 (continued)Abstract ClassesSlide 3Slide 4Slide 5Java Interfaces RevisitedSlide 7The ADTs List and Sorted List RevisitedImplementation of the ADT Sorted List That Used the ADT ListImplementations of the ADT Sorted List That Use the ADT ListJava Generics: Generic ClassesGeneric WildcardsGeneric Classes and InheritanceSlide 14Generic MethodsIteratorsSummarySlide 18Slide 19© 2006 Pearson Addison-Wesley. All rights reserved 9 B-1Chapter 9 (continued)Advanced Java TopicsCS102 Sections 51 and 52Marc Smith and Jim Ten EyckSpring 2007© 2006 Pearson Addison-Wesley. All rights reserved 9 B-2Abstract Classes•Example–CD player and DVD player•Both involve an optical disk•Operations–Insert, remove, play, record, and stop such discs© 2006 Pearson Addison-Wesley. All rights reserved 9 B-3Abstract ClassesFigure 9-8Figure 9-8CDP and DVDP have an abstract base class GDP© 2006 Pearson Addison-Wesley. All rights reserved 9 B-4Abstract Classes•Abstract classes–An abstract class is used only as the basis for subclasses•It defines a minimum set of methods and data fields for its subclasses–An abstract class has no instances–An abstract class should, in general, omit implementations except for the methods that•Provide access to private data fields•Express functionality common to all of the subclasses© 2006 Pearson Addison-Wesley. All rights reserved 9 B-5Abstract Classes•Abstract classes (Continued)–A class that contains at least one abstract method must be declared as an abstract class–A subclass of an abstract class must be declared abstract if it does not provide implementations for all abstract methods in the superclass© 2006 Pearson Addison-Wesley. All rights reserved 9 B-6Java Interfaces Revisited•A Java interface–Specifies the common behavior of a set of classes–Common uses•Facilitate moving from one implementation of a class to another–A client can reference a class’s interface instead of the class itself•Specify behaviors that are common to a group of classes© 2006 Pearson Addison-Wesley. All rights reserved 9 B-7Java Interfaces Revisited•Inheritance can be used to define a subinterface•The Java API provides many interfaces and subinterfaces–Example: java.util.Iterable•An iterator is a class that provides access to another class that contains many objects© 2006 Pearson Addison-Wesley. All rights reserved 9 B-8The ADTs List and Sorted List Revisited•BasicADTInterface–Can be used to organize the commonalities between the ADT list and the ADT sorted list–ListInterface•A new interface based on BasicADTInterface© 2006 Pearson Addison-Wesley. All rights reserved 9 B-9Implementation of the ADT Sorted List That Used the ADT List•Operations–createSortedList()–isEmpty():boolean {query}–size():integer {query}–sortedAdd(in newItem:ListItemType) throw ListException–sortedRemove(in anItem:ListItemType) throw ListException–removeAll()–get(in index:integer) throw ListIndexOutOfBoundsException–locateIndex(in anItem:ListItemType):integer {query}© 2006 Pearson Addison-Wesley. All rights reserved 9 B-10Implementations of the ADT Sorted List That Use the ADT List•A sorted list is a list–With an additional operation, locateIndex•A sorted list has a list as a member© 2006 Pearson Addison-Wesley. All rights reserved 9 B-11Java Generics: Generic Classes•ADT developed in this text relied upon the use of Object class•Problems with this approach–Items of any type could be added to same ADT instance–ADT instance returns objects•Cast operations are needed•May lead to class-cast exceptions•Avoid this issues by using Java generics–To specify a class in terms of a data-type parameter© 2006 Pearson Addison-Wesley. All rights reserved 9 B-12Generic Wildcards•Generic classes are not necessary related•Generic ? wildcard–Stands for unknown data type•Examplepublic void process(NewClass<?> temp) {System.out.println("getData() => " + temp.getData());} // end process© 2006 Pearson Addison-Wesley. All rights reserved 9 B-13Generic Classes and Inheritance•You can use inheritance with a generic class or interface•Method overriding rules–Declare a method with the same parameters in the subclass–Return type is a subtype of all the methods it overrides•It is sometimes useful to constrain the data-type parameter to a class or one of its subclasses or an implementation of a particular interface–To do so, use the keyword extends© 2006 Pearson Addison-Wesley. All rights reserved 9 B-14Abstract ClassesFigure 9-10Figure 9-10Sample class hierarchy© 2006 Pearson Addison-Wesley. All rights reserved 9 B-15Generic Methods•Method declarations can also be generic–Methods can use data-type parameters•Generic methods are invoked like regular non-generic methods•Examplepublic static <T extends Comparable<? super T>>void sort(ArrayList<T> list) {// implementation of sort appears here} // end sort© 2006 Pearson Addison-Wesley. All rights reserved 9 B-16Iterators•Iterator–Object that can access a collection of objects one object at a time–Traverses the collection of objects•JCF defines generic interface java.util.Iterator–And a subinterface ListIterator© 2006 Pearson Addison-Wesley. All rights reserved 9 B-17Summary•A subclass inherits all members of its previously defined superclass, but can access only the public and protected members•Subclasses and superclasses–A subclass is type-compatible with its superclass–The relationship between superclasses and subclasses is an is-a relationship•A method in a subclass overrides a method in the superclass if they have the same parameter declarations© 2006 Pearson Addison-Wesley. All rights reserved 9 B-18Summary•An abstract method in a class is a method that you can override in a subclass•A subclass inherits–The interface of each method that is in its superclass–The implementation of each nonabstract method that is in its superclass•An abstract class–Specifies only the essential members necessary for its subclasses–Can serve as the superclass for a family of classes© 2006 Pearson Addison-Wesley. All rights reserved 9 B-19Summary•Early (static) binding: compiler determines at compilation time the correct method to invoke•Late (dynamic) binding: system determines at execution time the correct method to invoke•When a method that is not declared final is invoked, the type of object is the determining factor under late


View Full Document

VASSAR CMPU 102 - Advanced Java Topics

Download Advanced Java Topics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Advanced Java Topics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Advanced Java Topics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?