Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Smarter Java development

Use interfaces to effectively enforce development contracts while maintaining loose coupling of code

  • Print
  • Feedback
A quick and simple scheme to speed the development of large-scale Java applications involves the use of interfaces. Java interfaces are a blueprint for the functionality contained in an associated object.

By incorporating interfaces into your next project, you will noticebenefits throughout the lifecycle of your development effort. The technique of coding to interfaces rather than objects will improve the efficiency of the development team by:

  • Allowing the development team to quickly establish the interactions among the necessary objects, without forcing the early definition of the supporting objects
  • Enabling developers to concentrate on their development tasks with the knowledge that integration has already been taken into account
  • Providing flexibility so that new implementations of the interfaces can be added into the existing system without major code modification
  • Enforcing the contracts agreed upon by members of the development team to ensure that all objects are interacting as designed


An overview

Because object-oriented development efforts involve the interactions of objects, it is essential to develop and enforce strong contracts between those objects. The technique of coding to interfaces involves using interfaces, rather than objects, as the primary method of communication.

This article will introduce the user to the concept of coding to interfaces through a simple example. A detailed example will follow, helping demonstrate the value of this scheme in a larger system requiring multiple developers. Before we get to the sample code, however, let's look at the benefits of coding to interfaces.

Why code to interfaces?

The Java interface is a development contract. It ensures that a particular object satisfies a given set of methods. Interfaces are used throughout the Java API to specify the necessary functionality for object interaction. Examples of interface use are callback mechanisms (Event Listeners), patterns (Observer), and specifications (Runnable, Serializable).

Coding to interfaces is a technique by which developers can expose certain methods of an object to other objects in the system. The developers who receive implementations of these interfaces have the ability to code to the interface in place of coding to the object itself. In other words, the developers would write code that did not interact directly with an object as such, but rather with the implementation of that object's interface.

Another reason to code to interfaces rather than to objects is that it provides higher efficiency in the various phases of a system's lifecycle:

  • Design: the methods of an object can be quickly specified and published to all affected developers
  • Development: the Java compiler guarantees that all methods of the interface are implemented with the correct signature and that all changes to the interface are immediately visible to other developers
  • Integration: there is the ability to quickly connect classes or subsystems together, due to their well-established interfaces
  • Testing: interfaces help isolate bugs because they limit the scope of a possible logic error to a given subset of methods


There is some overhead associated with this development technique, due to the required code infrastructure. This infrastructure includes both interfaces for the interactions between objects and invocation code to create implementations of interfaces. This overhead is insignificant when compared to the ease and benefit of using interfaces as described.

  • Print
  • Feedback

Resources