1CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)Lecture Set #13:Interfaces1. Interfaces2. WrappersCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)1Code Re-useMany operations recur in programmingsortingmax / min(These operations may apply to strings, numbers, etc.)Desirable: one implementation!Less codingLess likely to have typosEasier maintenance of code2CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)2PolymorphismUsing an interface we can create one variable that can reference objects of different types (i.e. UMStudent variable referencing CSMajor, CEMajor or PsychMajor)This form of “generalization” is called polymorphismHallmark of OO languagesAllows application of same code to objects of different typesPolymorphism: “A variable that takes on many shapes.”Interfaces: one mechanism Java provides for polymorphisma collection of prototypes (method prototypes but no bodies) akaabstract methodsA class C implements an interface I if C provides implementations of all of I’s abstract methods A class implementing an interface can also provide other methods or implement other interfacesCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)3WrappersWe may want to treat primitives as though they were objectsFor example, generic routines can be implemented using interfaces … but they are not usable on primitive typesTo overcome this problem, Java provides wrappers for primitive typesWrappers: classes whose objects contain single values of the “wrapped type”Wrappers also contain other useful conversion operations (to / from String, etc.)Wrappers included in java.lang:ByteShortIntegerLongFloatDoubleCharacterBoolean3CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)4The Integer WrapperThe documentation is on-line at http://java.sun.com/j2se/1.5.0/docs/api/NotesImmutableConstructorsImplements ComparableDocumentation says “Comparable<Integer>”Comparable in Java 5.0 is a interfaceHas compareTo method.CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)5In class Demo: Implementing a method using the Integer classCreate objects of type Integer using the constructorcan be based on int type values or variablesCreate an array of Integer type object references and those objects of type IntegerUse the API to access information about the data in the Integer classExpand this example to StringsExpand this example to Cats4CMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)6Adapting Cat to Implement ComparableThe Comparable Interface insists that I must implement compareTo method which has the following prototype:int compareTo(Object o)it must return a negative if the current object is less, a positive if the current object is greater or a 0 if they are thesame.What is Object?Type of all possible objects in any classShortcoming of (earlier) Java: no good way to say “same type as this”Instead: Implementation must take any objectCMSC 131 Fall 2007Jan Plane (Adapted from Bonnie Dorr)7What about int? char?Polymorphic findMin can be used on any class implementing ComparableWhat about primitive types (int, char, double, etc.)?They are not classesThey do not implement ComparableHence findMin cannot be used on themThat’s why we use
View Full Document