10. Pros and cons of providing an interface

Introducing an interface as well as a class declaration is preferable because:

  1. an interface declaration provides a clearer statement than a class declaration as to the contract (between the client and the supplier);
  2. writing the client mainly in terms of the interface means that it will be easier for the client to switch to a different implementation of a class;
  3. writing the client so that it only uses the interface and not the class implementing the interface means that it is not necessary to recompile the client when the class changes.

Cymerman ([6]) says: ‘The only drawbacks to this scheme are the fact that there is some overhead associated with the creation and casting of objects, and the fact that more code is required to first specify the interface and then code the class that implements the interface. These two drawbacks seem insignificant, though, compared to the vast number of benefits.’

The book 'The Java Programming Language' by Arnold, Gosling and Holmes ([1]) says: ‘Any major class you expect to be extended, whether abstract or not, should be an implementation of an interface. Although this approach requires a little more work on your part, it enables a whole category of use that is otherwise precluded.’ In their book, they produce a class with the header:

class AttributedBody extends Body implements Attributed

They say: ‘suppose we had created an Attributed class instead of an Attributed interface with an AttributedImpl implementation class. In that case, programmers who wanted to create new classes that extended other existing classes could never use Attributed, since you can extend only one class: the class AttributedBody could never have been created.’

In the book 'UML Distilled' ([7]), Martin Fowler writes: ‘Programming languages [other than Java] use a single construct, the class, which contains both interface and implementation. When you subclass, you inherit both. Using the interface as a separate construct is rarely used, which is a shame.’

In the 1990s, some useful work was done on understanding how complex systems are built: this work recognized the importance of the use of patterns. Currently, the principal book in this area is 'Design Patterns: Elements of Reusable Object-Oriented Software' by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides ([9]). These four people are affectionately known as the Gang of Four (or the GoF). In their book, they say: ‘This ... leads to the following principle of reusable object-oriented design:

Don't declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class [or an interface in Java]. You will find this to be a common theme of the design patterns in this book.’

On the WWW at [8], you will find an excellent document entitled 'ChiMu OO and Java Development: Guidelines and Resources'. Mark Fussell says: ‘Use interfaces as the glue throughout your code instead of classes: define interfaces to describe the exterior of objects (i.e., their Type) and type all variables, parameters, and return values to interfaces. The most important reason to do this is that interfaces focus on the client's needs: interfaces define what functionality a client will receive from an Object without coupling the client to the Object's implementation. This is one of the core concepts to OO.’

Stroustrup ([16]) says: ‘The importance of the abstract class concept is that it allows a cleaner separation between a user and an implementer than is possible without it. An abstract class is purely an interface to the implementations supplied as classes derived from it. This limits the amount of recompilation necessary after a change as well as the amount of information necessary to compile an average piece of code. By decreasing the coupling between a user and an implementer, abstract classes provide an answer to people complaining about long compile times and also serve library providers, who must worry about the impact on users of changes to a library implementation. I have seen large systems in which the compile times were reduced by a factor of ten by introducing abstract classes into the major subsystem interfaces.’