topics that matter; ideas worth sharing

share a tip, submit a link, add something new

The many interfaces of Java

December 14, 2000, 02:22 PM —  ITworld.com — 

In my last column, I derived my rule of interface from the requirements of an equation evaluator. Now that we're armed with the definition of the rule, let's tackle its implementation in Java.

You may recall that the rule of interface states the following:

  • An interface defines a protocol
  • Classes implement interfaces
  • Clients access objects via interfaces
  • Interfaces hide implementation

Java has four language constructs that obey the rule. These are:

  • The interface keyword
  • An abstract base class with public methods
  • An abstract base class with protected methods
  • A class with public methods

For reference, the example program we discussed in "Identity as
Information" includes applications of each construct. (Click href="http://mithras.itworld.com/articles/images/win-perry-0818-source.zip">here to
download source code.)

The interface keyword

The first feature shares the name of the rule itself. A Java interface, declared with
the interface keyword, represents a collection of
methods that define a protocol.

By using the implements keyword, a class pledges
to adhere to this protocol. If the class does not implement the protocol in its
entirety, it must be marked as abstract. Note that an
abstract class cannot be directly instantiated. It can, however, be extended,
ultimately resulting in a concrete class that fully implements the protocol.

In our example program, TextTarget defines an
interface. The class JCalcApplet implements the
TextTarget interface, and the size=+0>JCalcEngine class uses it. The rule of interface is in evidence
here, as the service defined by TextTarget is
separated from its implementation in JCalcApplet. The
client (JCalcEngine) relies on the functions to which
the interface definition allows access -- but it is not dependent on the specific
service implementation (i.e., JCalcApplet).
Instead, JCalcEngine can be used in any context.

Of all the Java language features that implement the rule of interface, the
interface keyword works best for size=+0>TextTarget. This is because size=+0>JCalcApplet already extends a base class (i.e., size=+0>Applet); it just needs to provide an additional service. The
creators of Java have wisely chosen to allow a class to extend one base class only;
thus, the language avoids the multiple-inheritance problems inherent to C++ -- namely,
the possibilities of inheriting conflicting implementations and creating grafted class
hierarchies. Java's interface keyword meets the need
for multiple interfaces much more safely than C++ mechanisms.

An abstract base class with public methods

Another Java language feature that programmers can use to define a protocol is the
abstract base class designation. A base class is labeled as size=+0>abstract, as are its public methods. As mentioned above, an
abstract class cannot be instantiated, but it can be extended; to implement the
interface, other classes extend the abstract base class.

In our example, the State class defines a
protocol. Derived classes such as StateStart
implement this protocol, and the client -- size=+0>JCalcEngine -- uses it. Here we are using the rule of interface
to gain interchangeability: since JCalcEngine sees
all state objects as equal, it can swap one object for another as it consumes input.
Recall that JCalcEngine consumes characters, which
are fed in one at a time. Characters are not explicitly stored; instead, each triggers
a state change that effectively records its information.

I chose to use an abstract base class with public methods (i.e., size=+0>State) in this case for two reasons. First, each instance of a
class derived from State exists solely to represent a
parser state; as mentioned, its entire mission is to consume one character, process the
input, and advance the machine. Other than State, no
other base class provides the services needed for these functions.

Second, all state objects have a bit of behavior in common: they all have access to
their owner, JCalcEngine. Using an abstract base
class instead of a Java interface, we can implement
that common behavior in one place -- the State
abstract base class.

An abstract base class with protected methods

Other than the obvious restrictions on access, Java makes no distinction between an
abstract base class with public methods and an abstract base class with protected
methods. I mention them separately, however, because their purposes are significantly
different. When methods are public, the implementer and the client are different
objects; when methods are protected, implementer and client are actually one and the
same. The Function, class, an example of an
abstract base class with protected methods, both defines a protocol and uses it.
Derived classes such as UserFunction implement
the protocol.

Here the rule of interface is evidenced in the separation of two fundamentally
different activities: parameter management and parameter evaluation. When we add a new
feature -- for example, trig functions -- we can reuse the generic parameter-management
capabilities of the Function class, while replacing
the specialized evaluation behavior of the size=+0>UserFunction class.

A class with public methods

The fourth Java language construct that implements the rule of interface is simply a
class with public methods. Such a class privately manages its internal state while
exposing a few well-designed public access points. You might also recognize this as the
object-oriented concept of encapsulation. Indeed, encapsulation is itself an aspect of
the law of interface: an interface encapsulates objects to hide implementation from
clients.

In our example, JCalcEngine both defines and
implements a protocol, while JCalcApplet uses it.
Although its structure in totality is quite complex, size=+0>JCalcEngine exposes only a few simple methods. Such abstraction
makes JCalcApplet easier to use as a service.

Making the choice to use a class with public methods in this case was simple. The
service that JCalcEngine provides is so specialized
that we have no reason to provide a second implementation. Thus, there's no advantage
to be gained from separating the interface from its implementation.

We have just discussed several ways in which Java adheres to the rule of interface.
Other mechanisms within Java warrant discussion, as well; we'll address them next time.
Java enthusiasts, be forewarned -- I will discuss both the merits and the drawbacks of
the language with equal bluntness.

» posted by abennett

ITworld.com

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

Get a broad understanding of important regulations and how you can make sure your site is in adherence.





Learn how VeriSign SGC-enabled SSL Certificates can help improve site security and customer confidence in the free white paper, "How to Offer the Strongest SSL Encryption." In this paper you will learn the differences between weak and strong encryption and what they mean for your site's performance.

Get VeriSign's free white paper: "The Latest Advancements in SSL Technology" and learn about the benefits of strong SSL encryption, Extended Validation (EV) SSL and security trust marks and what these SSL offerings can do for your site.

Now with Extended Validation (EV) SSL available from VeriSign, you can show your customers that they can trust your site. Learn about EV SSL benefits in this free VeriSign white paper.

More Resources