1 / 21

CSE 501N Fall ‘09 13: Interfaces and Multiple Representation

This lecture outlines the concept of interfaces in Java and discusses how multiple representations of data can be achieved. It covers the basics of interfaces, implementing interfaces in classes, and the use of interfaces in the Java standard class library.

smarcus
Download Presentation

CSE 501N Fall ‘09 13: Interfaces and Multiple Representation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 501NFall ‘0913: Interfaces and Multiple Representation 15 October 2009 Nick Leidenfrost

  2. Lecture Outline • Lab 4 questions? • Interfaces • Multiple Representation

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

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

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

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

  7. implements is a reserved word Each method listed in Doable is given a definition Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } [ Eclipse Example: Implementing ]

  8. Interfaces • A class that implements an interface can implement other methods as well • 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 ] • When a class implements an interface, it gains access to all its constants

  9. Interfaces • A class can implement multiple interfaces • The interfaces are listed in the implements clause, delimited by commas • The class must implement all methods in all interfaces listed in the header class ManyThings implements Interface1, Interface2 { // all methods of both interfaces }

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

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

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

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

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

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

  16. Multiple Representation • 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 • 5, V, |||||, five, 5.0, 0101 • 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!

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

  18. Multiple Representation

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

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

  21. Conclusion • Questions • Lab 5 Assigned • Due Next Thursday • Lab sessions now

More Related