5.6 Using Multiple Interfaces

 < Day Day Up > 



5.6 Using Multiple Interfaces

The PrintTable class can store objects of any class that implements the Printable interface. Now we will develop a table that can store these objects in some order. This table will add, delete, and print objects in the table. (Note that the example implements only the add method; the delete method is left for an exercise.) In order to add and delete sorted records, two methods must be available in the objects that will be stored. We will use gt for a method that returns true if the current record is greater than the record with which it is compared, and eq for a method that returns true if the two objects are equal. [3] We can now define an interface promise for our new SortedPrintTable. This interface would be:

   interface PrintableSortable {     public void print();     boolean equals(PrintableSortable ps);     boolean gt(PrintableSortable ps);   } 

Note that this interface is completely separate from the Printable interface. The Person class can now be written as implementing only a PrintableSortable interface as follows:

   class Person implements PrintableSortable {   } 

This definition has a problem in that the Person object is no longer Printable, and so is no longer usable in the PrintTable. Just because an object implements a method (in this case, print) does not mean that it implements an interface. It must also implement the correct interface. To make the Person object both Printable and PrintableSortable, it would have to be defined as follows:

   class Person implements Printable, PrintableSortable {   } 

This definition tells the components that use the Printable interface and the PrintableSortable interface that a print method is available for this object. A useful alternative defining a class with multiple overlapping interfaces is to create an interface that is a combination of other interfaces. For example, Exhibit 9 (Program5.4a) shows the definition for the PrintableSortable interface. Here, the PrintableSortable interface is a combination of the Printable and Sortable interfaces. Any class that implements this PrintableSortable interface also implements the Printable and Sortable interfaces. So, now the Person class must only implement the PrintableSortable interface to be used by components that require the Printable, Sortable, or PrintableSortable interface.

Exhibit 9: Program5.4a: PrintableSortable Definition

start example

 interface Printable {   public void print(); } interface Sortable {   public boolean gt(Sortable s);   public boolean eq(Sortable s); } public interface PrintableSortable extends Printable, Sortable { } 

end example

Note that the interfaces are combined using an extends clause, not an implements clause. This is sometimes confusing to programmers the first time they try creating interfaces from other interfaces; however, this is not an example of multiple inheritance in Java, as the definitions of the interfaces are combined and not really extended. Also, note that

the gt and eq methods take parameters of type Sortable. This is one of the advantages of defining the interface using the extends clause. Because a PrintableSortable object is both Printable and Sortable, only the relevant part of the interface needs to be used. If the PrintableSortable was defined with the gt, eq, and print methods, the gt and eq methods would have to use the PrintableSortable interface, and two distinct and different gt and eq methods defined, one for Sortables and one for PrintableSortables.

[3]Java has an equals function from class Object that is generally overridden to check for equals and a Comparable interface with a compareTo function to compare the ordering of two objects. However, the functions here were chosen to illustrate the behavior of interfaces, not to follow Java conventions.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

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