Defining an Interface
Purpose:
- To declare the responsibilities of an implementation.
Mechanics:
<modifiers> interface <interface name> {/* Abstract methods and class constants declared here */
}
Example:
public interface myInterface {int MY_CONSTANT = 10;void myMethod();
}
Usage:
- <modifiers> is an optional space-separated list of
valid interface modifiers. All interfaces are implicitly
abstractand inclusion of that modifier is optional. - <interface name>is any valid identifier which is unique to the current scope.
- All methods declared in an interface are implicitly
abstractandpublic. - All instance variables declared in an interface are implicitly
public, static,andfinal, and therefore must include an initial value in the declaration. - Inlcusion of implicit modifiers is optional.
- Variables may be declared of type <interface name> and defined to be of a class that implements the interface. Such references respond to all methods declared in the interface.
Restrictions:
- A method declared in an interface cannot have a default
implementation, nor can it be declared
private, protected, static, final,orsynchronized. An instance variable declared in an interface is considered a class constant. It cannot be declaredprivate, protected,orsynchronized.
email suggestions to: cs015tas@cs.brown.edu
