Declaring an Interface
An interface is a contract.Interfaces can be implemented by any class, from any inheritance tree. This lets you take radically different classes and give them a common characteristic. For example, you might want both a Ball and a Tire to have bounce behavior, but Ball and Tire don't share any inheritance relationship; Ball extends Toy while Tire extends only java.lang.Object. But by making both Ball and Tire
Think of an interface as a 100-percent abstract class. Like an abstract class,
an interface defines abstract methods that take the following form
But although an abstract class can define both abstract and nonabstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared.
■ All interface methods are implicitly public and abstract. In other words,
you do not need to actually type the public or abstract modifiers in the
method declaration, but the method is still always public and abstract.
■ All variables defined in an interface must be public, static, and final—
in other words, interfaces can declare only constants, not instance variables.
■ Interface methods must not be static.
■ Because interface methods are abstract, they cannot be marked final,
strictfp, or native. (More on these modifiers later in the chapter.)
■ An interface can extend one or more other interfaces.
■ An interface cannot extend anything but another interface.
■ An interface cannot implement another interface or class.
■ An interface must be declared with the keyword interface.
■ Interface types can be used polymorphically
The following is a legal interface declaration:
public abstract interface Rollable { }
Typing in the abstract modifier is considered redundant; interfaces are
implicitly abstract whether you type abstract or not. You just need to know that
both of these declarations are legal and functionally identical:
public abstract interface Rollable { }
public interface Rollable { }
The public modifier is required if you want the interface to have public rather
than default access.
We've looked at the interface declaration, but now we'll look closely at the
methods within an interface:
public interface Bounceable {
public abstract void bounce();
public abstract void setBounceFactor(int bf);
}
Typing in the public and abstract modifiers on the methods is redundant,
though, since all interface methods are implicitly public and abstract. Given that
rule, you can see that the following code is exactly equivalent to the preceding
interface:
public interface Bounceable {
void bounce(); // No modifiers
void setBounceFactor(int bf); // No modifiers
}
You must remember that all interface methods are public and abstract
regardless of what you see in the interface definition.
Look for interface methods declared with any combination of public, abstract,
or no modifiers. For example, the following five method declarations, if declared
within their own interfaces, are legal and identical!
void bounce();
public void bounce();
abstract void bounce();
public abstract void bounce();
abstract public void bounce();
final void bounce(); // final and abstract can never be used
// together, and abstract is implied
static void bounce(); // interfaces define instance methods
private void bounce(); // interface methods are always public
protected void bounce(); // (same as above)
Declaring Interface Constants
By placing
the constants right in the interface, any class that implements the interface has
direct access to the constants, just as if the class had inherited them. You need to remember one key rule for interface constants. They must always be
public static final
Because interface constants are defined in an interface, they don't have to be declared as public, static, or final. They must be public, static, and final, but you don't actually have to declare them that way.
Just as interface methods are always public and abstract whether you say so
in the code or not, any variable defined in an interface must be—and implicitly
is—a public constant. See if you can spot the problem with the following code
(assume two separate files):
interface Foo {
int BAR = 42;
void go();
}
class Zap implements Foo {
public void go() {
BAR = 27;
}
}

No comments:
Post a Comment