Interfaces


Comparable is an interface type, not a class type. An interface contains any number of method declarations. A method declaration is the signature for a method followed by a semicolon. There are no braces and there is no method code. Java defines the type Comparable as follows:

 public interface Comparable<T> {    public int compareTo(T o); } 

The letter T is a placeholder for the type of objects to be compared. Lesson 14 on generics will explain this concept in further detail. For now, understand that you declare a Comparable reference along with a type. The Java compiler substitutes this type for all occurrences of T in the interface declaration.

Contrast an interface declaration to a class declaration. You declare an interface using the interface keyword, whereas you declare a class using the class keyword. An interface can contain no implementations for any of the methods that it declares.

In UML, you can represent an interface in a number of ways. Figure 5.1 shows one such way.

Figure 5.1. Comparable


The goal of the compareTo method is to return a value that indicates whether an object should come before or after another object with respect to ordering. All sort techniques involve comparing only two objects at a time and swapping those two objects if necessary.

For example, a list contains two String objects, "E" and "D", in that order. You want the list sorted in alphabetical order. The Collections method sort would send the compareTo message to the String "E", along with the parameter "D". The compareTo method would return a value that indicates that "D" should appear first in the sort order, before "E". The sort method would then know that it would have to swap the two objects for them to be sorted correctly.

A class can declare that it implements an interface. By making this declaration, a class promises to provide implementations for each method declared in the interface. Implementing an interface allows classes to act as more than one type.

The earlier sorting example, the one that actually worked, sorted a collection of String objects. The J2SE API documentation for the String class lists the Comparable interface as one of three implemented interfaces. Implementing the Comparable interface allows String objects to be treated not just as objects of String type but also as objects of Comparable type.

The syntax for implementing interfaces is demonstrated in this bit of code from the actual Java String class:

 public final class String     implements java.io.Serializable, Comparable<String>, CharSequence { ... 

If you look further into the source for the String class, you will find the implementation for the compareTo method that Comparable declares.

For the sort invoked by CourseReport to work, it must be able to send the message compareTo to the objects contained within sessions. The sort method is unaware of the class of objects in the collection, however. It takes each object in turn and tries to assign the object to a variable of the Comparable interface type. When this assignment is successful, the sort can safely send the compareTo message to the object. When the assignment is not successful, Java generates an error.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

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