Referencing Interfaces

   

You have learned how to create interfaces and build classes based on interfaces. However, interfaces are not useful unless you can develop classes that will either employ the derived classes or the interface itself.

Accessing Constants

Although the fields of an interface must be both static and final, they can be extremely useful in your code.

The following example demonstrates that any constant from an interface can be referenced using the same dot notation you use with classes. This means that you can use constants from your own interfaces or those in the Java API such as java.awt.image.ImageConsumer.COMPLETESCANLINES and java.awt.Event.MOUSE_DOWN in your code. Listing 9.7 shows an example of another ImageConsumer field being used.

Listing 9.7 Using the Constant Fields of an Interface
 import java.awt.image.*; public class MyImageHandler {   /* The java.awt.image.ImageConsumer interface defines certain      constants to serve as indicators. STATICIMAGEDONE, which      is set to equal 3, informs the consumer that the image is complete.    */   ImageConsumer picture;   void checkStatus(boolean done) {     if (done)       picture.imageComplete(ImageConsumer.STATICIMAGEDONE);   } } 

Design to an Interface Rather Than an Implementation

The most important concept to take away from a discussion of interfaces is that you should develop a mindset of designing your systems based on contracts specified by interfaces rather than specific class hierarchies and implementations . If you establish the interfaces in your system early in the design process, you greatly reduce the likelihood of rewriting the implementations as a result of incompatibilities between system components . Establishing the interfaces is a way to clearly draw the lines of responsibility between classes.

When you design and develop using interfaces, you are creating reference types that can be used as any other data type even though an interface will never itself be instantiated . An interface variable can be used just as you would a variable of any class type, except that you cannot use a new statement, or any other means of object creation, on an interface type.

An Interface as a Parameter Type

In Listing 9.8, you create a simple application that employs the JavaBook class developed earlier. Because the JavaBook class implements the Sellable interface, you can deal with an instance of this class either as a JavaBook object or as an object that implements the Sellable interface. Although both approaches produce the same results when the methods accessed are all from the underlying interface, accessing the instance through the Sellable interface provides you with a more flexible approach. Here an instance of Boeing767, or any other Sellable class, could just as easily be passed to the getPrice method. This method doesn't care if it is working with a commercial airliner or a book, so there is no reason to restrict its use by specifying a particular class implementation the way the getInfo method does.

Listing 9.8 Using the Sellable Interface as a Parameter Type
 public class Store {   static JavaBook programmersGuide;   public static void init() {     programmersGuide = new JavaBook();   }   public static void main(String argv[]) {     init();     getInfo(programmersGuide);     getPrice(programmersGuide);   }   public static void getInfo(JavaBook item) {     System.out.println("Product Description: "+ item.getDescription());   }   public static void getPrice(Sellable item) {     System.out.println("Price: "+ item.getPricePerUnit());   } } 

Notice that in treating programmersGuide as an instance of the Sellable interface, you are asking the compiler to perform an implicit type conversion for you. This can be done at compile time because JavaBook implements Sellable. A class instance can always be converted to any interface it implements or one of its superclasses. You could explicitly cast programmersGuide to Sellable in the call to getPrice, but it would be redundant and a potential runtime error if the class and interface relationships were to change later.

Although it is not necessary to use the Sellable type as your argument in this simplistic example, the advantages of its use become apparent when you have a large number of classes that implement an interface. A method should accept parameters of as general a type as possible so that it can be more easily reused. This example would quickly become unwieldy if a different version of getPrice had to be created for each type of product encountered by the system.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net