Java Reference
In-Depth Information
PITFALL: (continued)
As far as the compiler and run-time systems are concerned, the Ordered interface
merely says that the methods precedes and follows each take one argument of type
Object and return a boolean value. The interface does not really require that the
boolean value be computed in any particular way. For example, the compiler would
be satisfied if both precedes and follows always return true or if they always return
false . It would even allow the methods to use a random number generator to gener-
ate a random choice between true and false .
It would be nice if we could safely give an interface some simple semantics, such as
saying that o1.follows(o2) means the same as o2.preceded(o1) . However, if Java did
allow that, there would be problems with having a class implement two interfaces or
even with having a class derived from one base class and implementing an interface.
Either of these situations could produce two semantic conditions both of which must be
implemented for the same method, and the two semantics may not be consistent. For
example, suppose that (contrary to fact) you could require that o1.follows(o2) means
the same as o2.preceded(o1) . You could also define another interface with an inconsis-
tent semantics, such as saying that precedes always returns true and that follows
always returns false . As long as a class can have two objects, there is no way a class could
implement both of these semantics. Interfaces in Java are very well behaved, the price of
which is that you cannot count on Java to enforce any semantics in an interface.
If you want to require semantics for an interface, you can add it to the documentation, as
illustrated by the comment in Display 13.1 and the comment in Display 13.4, but always
remember that these are just comments; they are not enforced by either the compiler or the
run-time system, so you cannot necessarily rely on such semantics being followed. However,
we live in an imperfect world, and sometimes you will find that you must specify a seman-
tics for an interface; you do so in the interface's documentation. It then becomes the respon-
sibility of the programmers implementing the interface to follow the semantics.
Having made our point about interface semantics not being enforced by the com-
piler or run-time system, we want to nevertheless urge you to follow the specified
semantics for an interface. Software written for classes that implement an interface will
assume that any class that implements the interface does satisfy the specified seman-
tics. So, if you define a class that implements an interface but does not satisfy the
semantics for the interface, then software written for classes that implement that inter-
face will probably not work correctly for your class.
Interface Semantics Are Not Enforced
When you define a class that implements an interface, the compiler and run-time system will
let you define the body of an interface method any way you want, provided you keep the
method heading as it is given in the interface. However, you should follow the specified
semantics for an interface whenever you define a class that implements that interface; other-
wise, software written for that interface may not work for your class.
Search WWH ::




Custom Search