Interfaces

advertisement
Lecture 16 - Interfaces
Professor Adams
Interfaces are
 “Java’s substitute for C++’s feature of
multiple inheritance… “
 “…used when you want to define a certain
functionality to be used in several classes,
but are not sure exactly how this
functionality will be defined by those
classes…”

Joe Weber and Mike Afergan in Using Java 2nd Edition
Placing classes in an interface
 outlines common behavior
 leaves specific implementation to the
classes themselves
 Makes using interfaces instead of classes a
better choice when dealing with advanced
data handling.

Joe Weber and Mike Afergan in Using Java 2nd Edition
Interfaces
 are underprivileged first cousins of classes
 define a set of methods and constants to be
implemented by another object
 help define the behavior of an object by declaring
a set of characteristics for the object
 cannot specify any implementation for methods

Joe Weber and Mike Afergan in Using Java 2nd Edition
Interfaces
 A java interface is a collection of constants and
abstract methods
 Remember: an abstract method is a method that
does not have an implementation (a body)
 An interface can not be instantiated
 Although abstract methods can be preceded by the
reserved word abstract, in interfaces they are
generally not.

Lewis & Loftus pp. 309-310
Implementation of an Interface
 “A class implements an interface by providing
method implementations for each of the abstract
methods defined in the interface.”
 “A class that implements an interface uses the
reserved word implements followed by the class
name in the class header.”
 If a class asserts that it implements a particular
interface, it must provide a definition for all of the
methods in the interface or a compile error will
occur.

Lewis & Loftus, p. 310
Classes that implement an interface
 are responsible for specifying the
implementation of the methods in the
interface
 must override every method in the interface
An Example is on next slide and code is in
directory.
Example and notes

public interface Product
{
static final String MAKER = "My Corp";
static final String PHONE = "555-123-4567";
public int getPrice (int id);
}
 file is named Product.java
 getPrice is an abstract method – doesn’t require
the word abstract and we will omit it.
 getPrice has no body (nor can it have one)
 When attributes (fields) are present they must be
static and are final
Shoe.java
public class Shoe implements Product
{
public int getPrice (int id)
{
if (id == 1)
return (5);
else
return (10);
}
public String getMaker()
{
return (MAKER);
}
}
Store.java

public class Store
{
static Shoe hightop;
public static void main (String arg[] )
{
init();
getInfo (hightop);
orderInfo(hightop);
}
public static void init ()
{
hightop = new Shoe();
}
public static void getInfo (Shoe item)
{
System.out.println
(" This Product is main by " + item.MAKER);
System.out.println
(" It costs $" + item.getPrice(1) + '\n');
}
public static void orderInfo (Product item)
{
System.out.println
(" To order from " + item.MAKER + " call " + item.PHONE + ".");
System.out.println
(" Each item costs $" + item.getPrice(1));
}
} // end class Store
When you create an interface




None of your methods may have a body
No non-constant variables may be declared
Our interfaces will all be public
Interface filename must match interface
name
extending
 Interfaces can not extend classes
 Interfaces can only extend other interfaces
 If you implement an extended interface, you
must override both the methods in the new
interface and the methods in the old
interface
Remember these
 Classes implement interfaces to inherit their
properties
 Methods in classes must have bodies unless they
are abstract methods (in which case they are in
abstract classes)
 Syntax of a method declaration in an interface is
public return_value nameOfMethod (parameterList) throws
ExceptionList ;
Interfaces
 “An interface is similar to a class but there
are several important differences:




All methods in an interface are abstract; that
is, they have name, parameters, and a return
type but they don’t have an implementation.
All methods in an interface are automatically
public
An interface doesn’t have instance variables.”
Horstman, p. 365
Common Error with interfaces
 Forgetting to define implementing methods
as public
Programs from text
 //**********************************************
* Complexity.java – p. 310
*
* @Author: Lewis/Loftus
* The interface for an object that can be assigned an
* explicit complexity.
*/
public interface Complexity
{
public void setComplexity (int complexity);
public int getComplexity();
}
Interfaces
 A class can implement more than one interface. In these
cases, the class must provide an implementation for all
methods in all interfaces listed.
 To show that a class implements multiple interfaces, they
are listed in the implements clause, separated by commas.
 Here is an example:
Class ManyThings implements interface1, interface2,interface3
{
// contains all methods of all interfaces
}
Interfaces in the Java standard class library
 The Comparable interface is defined in java.lang and
contains only one method compareTo which takes an
object as a parameter and returns an integer.
 Documentation indicates that integer will be negative if
obj1 is less than obj2; 0 if obj1 = = obj2; and positive if
obj1 > obj2.
 Here’s an example of an invocation of the method.
If (obj1.compareTo(obj2) < 0)
System.out.println (“ obj1 is less than obj2”);
 The String class implements the Comparable interface and
has a compareTo method.

Lewis & Loftus, pp. 315-316
Interfaces in the Java standard class library
 The Iterator interface is another interface defined
as part of the Java standard class library. It is used
by a class that represents a collection of objects,
providing a means to move through the collection
one at a time.
 The two primary methods in the Iterator interface
are:



hasNext – which returns a boolean result
next – which returns an object
Lewis & Loftus, pp. 315-316
Download