- Forward


Interfaces
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Basics
Back SMYC Forward
  • Purpose:
    • Provide a view of a set of services provided by a class
  • Interpretations:
    • An interface is a "contract"
    • A class that realizes an interface promises that it will implement all of the methods in the interface
Interfaces vs. Classes
Back SMYC Forward
  • Classes:
    • The set of objects is defined by both attributes and methods
    • Methods have declarations (i.e., the "what") and bodies (i.e., the "how")
  • Interfaces:
    • The set of objects is defined by methods only
    • Methods are abstract (i.e., only have declarations not bodies)
Interfaces vs. Abstract Classes
Back SMYC Forward
  • A Likely Question If You Know About Both:
    • Isn't an interface just an abstract class in which all of the methods are abstract?
  • An Answer:
    • They are similar, but interfaces don't involve inheritance and, hence, are simpler and more flexible (see the discussion of multiple inheritance below)
UML
Back SMYC Forward
  • Visualization:
    • interfaces_visualization
  • Conventions:
    • Abstract methods should be italicized but, because all methods in an interface are abstract, the italics are sometimes omitted
    • A class that realizes an interface (typically) does not explicitly list the methods in the interface
Java Details
Back SMYC Forward
  • Syntax:
    • accessibility interface name { body }
  • Contents of the body:
    • Only method declarations (with the abstract modifier) terminated by a ;
  • Concrete Classes that Realize an Interface:
    • Declaration: accessibility class name implements iface1[, iface2]...
    • Implementation: Must implement all methods (in all interfaces)
An Example
Back SMYC Forward
  • An Observation:
    • We often neede to find the min/max of the elements in an array and we have had to implement a method for that purpose for each different element type
  • A Solution:
    • Create an interface with the requirements that all ordered objects must have
An Example (cont.)
Back SMYC Forward

The Interface

javaexamples/oopbasics/interfaces/Ordered.java
 
An Example (cont.)
Back SMYC Forward

A Class that Implements the Interface

javaexamples/oopbasics/interfaces/Person.java
 
An Example (cont.)
Back SMYC Forward

The min()/max() Methods

javaexamples/oopbasics/interfaces/Statistics.java
 
An Example (cont.)
Back SMYC Forward

An Application

javaexamples/oopbasics/interfaces/PersonDriver.java
 
Design Considerations
Back SMYC Forward
  • A Shortcoming of this Design:
    • The object that is returned must be typecast
  • Two Alternative Designs to Consider:
    • Have min() and max() return the index
    • Use an outbound parameter
Design Considerations (cont.)
Back SMYC Forward
  • Shortcomings of Returning an Index:
    • Can't use a variable-length parameter list (because the returned index is only meaningful in the context of an array)
  • Shortcomings of an Outbound Parameter:
    • Parameters are passed by value so it would need to be wrapped
A Larger Example
Back SMYC Forward
  • The Setting:
    • We're developing an emergency messaging system
    • We have two kinds of text messages
    • We now have an accident report
    • The operator needs to be able to distinguish high and low priority messages
  • The UML Model:
    • emergency-messaging-overview
A Larger Example (cont.)
Back SMYC Forward

The Interface

javaexamples/oopbasics/emergency2/Prioritized.java
 
A Larger Example (cont.)
Back SMYC Forward

The EmergencyMessage Base Class Revisited

javaexamples/oopbasics/emergency2/EmergencyMessage.java
 
A Larger Example (cont.)
Back SMYC Forward

The Alert Derived Class Revisited

javaexamples/oopbasics/emergency2/Alert.java
 
A Larger Example (cont.)
Back SMYC Forward

A Prioritized that does not Specialize EmergencyMessage

javaexamples/oopbasics/emergency2/AccidentReport.java
 
A Larger Example (cont.)
Back SMYC Forward

An Application

Note that this application uses an array of Prioritized objects (which can contain EmergencyMessage objects, Alert objects, and AccidentReport objects).

javaexamples/oopbasics/emergency2/Driver.java
 
Implementing Multiple Interfaces
Back SMYC Forward
  • Is it Allowed in Java?
    • Yes, a class can implement multiple interfaces
  • Isn't this Like Multiple Inheritance?
    • No, a class that implements multiple interfaces simply promises to have all of the methods -- no "confusion" about which parent to inherit from can arise because nothing is inherited
Specializing Interfaces
Back SMYC Forward
  • A Situation:
    • You notice that the interface B contains all of the methods in the interface A (and more)
  • Using Specialization:
    • Remove the duplicate method signatures and have the interface B specialize the interface A
Specializing Interfaces (cont.)
Back SMYC Forward
  • UML:
    • interfaces_specialization
  • Java:
    • public interface B extends A {...}
Constructors in Interfaces
Back SMYC Forward
  • Instantiating an Interface:
    • Does not make sense
  • Classes that Implement an Interface:
    • Each needs to have its own constructors
Uses for Interfaces
Back SMYC Forward
  • Describing Similarities:
    • Classes that are not related hierarchically can still have similar behaviors and, as a result, be treated in a similar fashion
  • Anonymous Objects:
    • It is possible to reveal the capabilities of an object without revealing its class
Base/Derived Classes and Interfaces
Back SMYC Forward
  • Can a Class Both extends and implements?
    • Yes, a derived class can extend a base class and implement one or more interfaces
    • An Example: Alert extends EmergencyMessage and could implement one or more interfaces
  • Are Interface Realizations Inherited?
    • Yes, since a derived class inherits all of the methods in the base class, the derived class implements all of the interfaces of the base class
    • An Example: Alert realizes the Prioritized interface
Interfaces in the Java API
Back SMYC Forward
  • Documentation Conventions:
    • Are in the class list in italics
  • The FileFilter Interface:
    • The FileFilter interface consists of a single boolean accept(File) method
    • An object that implements this interface can be passed to the File[] listFiles(FileFilter) method in the File class to control its behavior
Returning to the Larger Example
Back SMYC Forward

Suppose we want to be able to find the AccidentReport involving the largest number of vehicles.

javaexamples/oopbasics/emergency3/AccidentReport.java
 
Returning to the Larger Example (cont.)
Back SMYC Forward

An Application

javaexamples/oopbasics/emergency3/Driver.java
 
Returning to the Larger Example (cont.)
Back SMYC Forward
  • A Different Requirement:
    • Suppose the product was required to find the object with the highest priority
  • Incorporating this Functionality:
    • The compareTo() method could use the result of a call to getPriority() rather than the size of the accident
    • The compareTo() method could be moved to the base EmergencyMessage class and the typecast would be to a Prioritized and a compareTo() method would have to be added to the AccidentReport class
  • Design Considerations:
    • Should the Prioritized interface extend the Ordered interface?
    • How troubling is it that there are identical compareTo() methods in EmergencyMessage and AccidentReport? Should specialization be used to eliminate this code duplication?
There's Always More to Learn
Back -