What Are Interfaces?

   

A Java interface is a set of constants and method declarations without an implementation, as shown in Listing 9.1.

Listing 9.1 Steerable.java ” An Interface for Steerable Vehicles
 public interface Steerable {   String NORTH_DIRECTION = "North";   String SOUTH_DIRECTION = "South";   String EAST_DIRECTION  = "East";   String WEST_DIRECTION  = "West";   void turnLeft();   void turnRight();   String getCurrentDirection(); } 

This concept might not sound too impressive at this point, but interfaces are a powerful feature of the Java language. Successful object-oriented programming depends on separating the functionality needed by one part of your system from how that functionality is ultimately implemented by another part. Interfaces are the way to achieve that separation.

In the preceding example, the Steerable interface defines three operations that a class responsible for sending steering commands to some type of vehicle in a system cares about. Notice that there is nothing in the interface that references any details about a particular type of vehicle or what it has to do to respond to a steering command. A vehicle that wants to allow itself to be steered in this manner must provide an implementation for these methods , but it does not have to supply any details about this implementation or any other part of its operation.

You should think of an interface as a contract. The Steerable contract is satisfied when both sides agree to its terms. A class that wants to steer a vehicle does this by using only those methods in the interface to issue its commands. A vehicle class that can be steered is obligated to provide a valid implementation for each one. By doing this, a class is said to implement the interface. A vehicle class that can only turn left cannot satisfy this contract, unless maybe it implements a right turn as a series of three left turns. This wouldn't be very efficient, but the Steerable interface only cares that a vehicle be able to make a right turn, not that it be able to do it in any particular way. If the behavior of this class later changed so that its implementation of the method needed to be modified, any code that relied on the interface would be unaffected. Placing your implementations behind interfaces gives you the freedom to change them without breaking the contract agreed upon in advance.

To summarize, interfaces allow you to focus on behavior without being concerned about implementation details. They also allow you to relate classes with similar behavior without forcing a class inheritance hierarchy. This aspect is key in developing flexible designs.

Doesn't Every Class Already Have an Interface?

The idea of interfaces is similar to encapsulation and implementation hiding in general. Through the proper use of access restrictions, you can already limit other classes from having knowledge of how the behavior of a class is implemented within its methods. This typically includes restricting the use of any intermediate and utility methods that are part of a class implementation. From this perspective, every class already has a well-defined interface. All the fields and methods of a class that are accessible to other classes make up its interface in this sense. There is a significant difference between this concept and the use of a Java interface however. The interface defined by the fields and methods of a class is specific to an implementation. This is true even if most of the details of that implementation are hidden. A declared interface, such as the Steerable example, decouples you from any particular implementation because it, in fact, has no implementation.

Multiple Inheritance

A class can extend only one class in Java, but it can implement multiple interfaces. This is the closest Java comes to the true multiple inheritance found in languages such as C+. Java was designed to build on the strengths of C++ and at the same time, avoid some of its features that proved troublesome .

In practice, multiple inheritance typically produces less glamorous results than it does in theory. This is especially true when class hierarchies result such that a class appears twice in the inheritance chain for a subclass. For example, if classes B and C both inherit from A, and class D is declared to inherit from B and C, how do you deal with the two versions of the superclass A ? Careful design can prevent these situations, but designs that favor composition over complex inheritance hierarchies tend to produce much more scalable and robust solutions. A common problem for programmers new to an object-oriented language is to go overboard with inheritance. When a class implements an interface, it is stating that it supports a specific behavior, not that it is a subtype of some other type. Implementing multiple interfaces allows a class to provide a robust set of behavior without being tied to any superclass hierarchy whatsoever.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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