Java Reference
In-Depth Information
is just one aspect of what you can do using an interface. I start by examining what an interface is from the
ground up and then look at what you can do with it.
An interface is essentially a collection of related constants and/or abstract methods, and in most cases it
contains just methods. An interface doesn't define what a method does. It just defines its form — its name,
its parameters, and its return type, so by definition the methods in an interface are abstract.
To make use of an interface, you implement the interface in a class — that is, you declare that the class
implements the interface using the implements keyword and you write the code for each of the methods that
the interface declares as part of the class definition. When a class implements an interface, any constants that
were defined in the interface definition are available directly in the class, just as though they were inherited
from a base class. An interface can contain either constants, or abstract methods, or both.
The methods in an interface are always public and abstract , so you do not need to specify them as
such; it is considered bad programming practice to specify any attributes for them, and you definitely cannot
add any attributes other than the defaults, public and abstract . This implies that methods declared in an
interface can never be static , so an interface always declares instance methods. The constants in an inter-
face are always public , static , and final , so you do not need to specify the attributes for these either.
An interface is defined just like a class, but using the keyword interface rather than the keyword class .
You store an interface definition in a . java file with the same name as the interface. The name that you give
to an interface must be different from that of any other interface or class in the same package. Just as for
classes, the members of the interface — the constants and/or method declarations — appear between a pair
of braces that delimit the body of the interface definition.
Encapsulating Constants in a Program
You will often find that a program makes use of a set of constant values that you really want to define only
once. You might have values representing standard colors that your program uses or perhaps constants that
are used in calculations such as conversion factors from one set of units to another. In Java versions prior
to 5.0, a common approach was to define a set of related constants in an interface and then implement the
interface in any class that used any of the constants. This approach has largely been made obsolete by the
static import capability.
The capability to import static members of a class that was introduced in Java 5 provides an excellent
way of dealing with constants in a program. However, the use of an interface for such purposes was very
widespread prior to Java 5, so I first explain briefly how that works to equip you for when you run into it. I
then explain how you use static import to access constants that you have defined in a class, which is a much
cleaner and better way of making a set of constants available wherever they are needed.
Constants in an Interface
Suppose you are writing a program that converts measurements between metric and imperial units. Here's
how the constants that such a program might use could be defined in an interface:
public interface ConversionFactors {
double INCH_TO_MM = 25.4;
double OUNCE_TO_GRAM = 28.349523125;
double POUND_TO_GRAM = 453.5924;
double HP_TO_WATT = 745.7;
double WATT_TO_HP = 1.0/HP_TO_WATT;
Search WWH ::




Custom Search