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 abstract and 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 abstract and public.
  • All instance variables declared in an interface are implicitly public, static, and final, 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, or synchronized. An instance variable declared in an interface is considered a class constant. It cannot be declared private, protected, or synchronized.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu