CSE 501 N Fall 09 13 Interfaces and

  • Slides: 21
Download presentation
CSE 501 N Fall ‘ 09 13: Interfaces and Multiple Representation 15 October 2009

CSE 501 N Fall ‘ 09 13: Interfaces and Multiple Representation 15 October 2009 Nick Leidenfrost

Lecture Outline n Lab 4 questions? n Interfaces n Multiple Representation 2

Lecture Outline n Lab 4 questions? n Interfaces n Multiple Representation 2

Interfaces n A Java interface is a collection of abstract methods and constants n

Interfaces n A Java interface is a collection of abstract methods and constants n An abstract method is a method header without a method body n An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off n An interface is used to establish a set of methods that a class will implement 3

Interfaces interface is a reserved word public interface Doable { None of the methods

Interfaces interface is a reserved word public interface Doable { None of the methods in an interface are given a definition (body) public void do. This(); public int do. That(); public void do. This. Too (float value, char ch); public boolean do. The. Other (int num); } [ Eclipse Example: Creation ] A semicolon immediately follows each method header 4

Interfaces n An interface cannot be instantiated ¨ However, we can have variables with

Interfaces n An interface cannot be instantiated ¨ However, we can have variables with interface types: Doable a. Doable n = new Can. Do(); Methods in an interface have public visibility by default ¨ If you omit an access modifier, public is used, not default (package) protected n ¨ default, protected, and private modifiers not permitted Why does it make sense for an interface only to offer public methods? 5

Interfaces n A class formally implements an interface by: ¨ Stating n so in

Interfaces n A class formally implements an interface by: ¨ Stating n so in the class declaration header Using the implements keyword ¨ providing implementations for each abstract method in the interface n If a class asserts that it implements an interface, it must define all methods in the interface 6

Interfaces public class Can. Do implements Doable { public void do. This () {

Interfaces public class Can. Do implements Doable { public void do. This () { // whatever implements is a } reserved word public void do. That () { // whatever } Each method listed in Doable is given a definition // etc. } [ Eclipse Example: Implementing ] 7

Interfaces n A class that implements an interface can implement other methods as well

Interfaces n A class that implements an interface can implement other methods as well n In addition to (or instead of) abstract methods, an interface can contain constants ¨ Any variables declared inside an interface are, by default, static final [ Eclipse Example ] n When a class implements an interface, it gains access to all its constants 8

Interfaces n A class can implement multiple interfaces n The interfaces are listed in

Interfaces n A class can implement multiple interfaces n The interfaces are listed in the implements clause, delimited by commas class Many. Things implements Interface 1, Interface 2 { // all methods of both interfaces } n The class must implement all methods in all interfaces listed in the header 9

Interfaces n The Java standard class library contains many helpful interfaces n The Comparable

Interfaces n The Java standard class library contains many helpful interfaces n The Comparable interface contains one abstract method called compare. To, which is used to compare two objects n The String class implements Comparable, giving us the ability to put strings in lexicographic order 10

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type n if (obj 1. compare. To(obj 2) < 0) System. out. println ("obj 1 is less than obj 2"); n The value returned from compare. To should be negative if obj 1 is less that obj 2, 0 if they are equal, and positive if obj 1 is greater than obj 2 n When a programmer designs a class that implements the Comparable interface, it should follow this intent 11

The Comparable Interface n It's up to the programmer to determine what makes one

The Comparable Interface n It's up to the programmer to determine what makes one object less than another n For example, you may define the compare. To method of an Employee class to order employees by name (alphabetically) or by employee number n The implementation of the method can be as straightforward or as complex as needed for the situation 12

The Iterator Interface n An iterator is created formally by implementing the Iterator interface,

The Iterator Interface n An iterator is created formally by implementing the Iterator interface, which contains three methods n The has. Next method returns a boolean result – true if there are items left to process n The next method returns the next object in the iteration n The remove method removes the object most recently returned by the next method 13

The Iterator Interface n By implementing the Iterator interface, a class formally establishes that

The Iterator Interface n By implementing the Iterator interface, a class formally establishes that objects of that type are iterators n The programmer must decide how best to implement the iterator functions ¨ It depends on the data (structure) we are iterating over [ Java Library: Iterable ] n Once established, the for-each version of the for loop can be used to process the items in the iterator 14

Interfaces n You could write a class that implements certain methods (such as compare.

Interfaces n You could write a class that implements certain methods (such as compare. To) without formally implementing the interface (Comparable) n However, formally establishing the relationship between a class and an interface allows Java to deal with an object in certain ways n Interfaces are a key aspect of object-oriented design in Java 15

Multiple Representation n There are multiple ways to represent almost any kind of data,

Multiple Representation n There are multiple ways to represent almost any kind of data, even all the way down to the seemingly most primitive. ¨ E. g. , the number 5 can be represented as n 5, V, |||||, five, 5. 0, 0101 n The choice of representation of numbers has great impact on the efficiency with which computations may be carried out. ¨ For example, people today generally use the decimal system ¨ If we still used the roman numeral system, imagine how much more difficult it would be to make technological progress! 16

Multiple Representation n When designing software, we too have a choice of representations n

Multiple Representation n When designing software, we too have a choice of representations n Suppose we want to model a set ¨ For n n internal representation we can choose Arrays Linked. Lists Others? How does this look conceptually? 17

Multiple Representation 18

Multiple Representation 18

Multiple Representation n Use an interface to capture the essential elements that must be

Multiple Representation n Use an interface to capture the essential elements that must be common across all representations ¨ (Usually n which methods must be offered) Have concrete implementations (classes) implement the interface ¨ Details of implementation are abstracted away 19

Example: Processing Data The Data. Provider interface abstracts away the details of the implementation

Example: Processing Data The Data. Provider interface abstracts away the details of the implementation of how the data is being provided You Application (Data Processor) Data Provider The application can interact with all kinds of data providers uniformly, without needing to know which is providing the data. Network Connection Person at Computer File 20

Conclusion Questions n Lab 5 Assigned n ¨ Due n Next Thursday Lab sessions

Conclusion Questions n Lab 5 Assigned n ¨ Due n Next Thursday Lab sessions now 21