SCJP Study Guide:
Declarations, Initialization and Scoping

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents


What is a Interface?

In Java, an interface is a reference type, just as a class is a reference type. Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. An interface defines a public API.

An interface defines a contract and a class that implements an interface must adhere to its contract. A class can implement multiple interfaces (but a class can only extend from one class).

Interfaces provide a mechanism that allow hierarchically unrelated classes to implement the same set of methods. Basically, an interface is a collection of method definitions and constant values that is free from dependency on a specific class or class hierarchy. Interfaces are useful for:

  • declaring methods that one or more classes are expected to implement
  • revealing an object's programming interface without revealing its class (objects such as these are called anonymous objects and can be useful when shipping a package of classes to other developers)
  • capturing similarities between unrelated classes without forcing a class relationship
  • Modeling multiple inheritance, a feature of some object-oriented languages that allows a class to have more than one superclass

Interface Declaration

An interface declaration looks like this:

[InterfaceModifiers] interface InterfaceName [extends listOfSuperInterfaces] {
    . . .//interfaceBody

At minimum, the interface declaration contains the Java keyword interface and the name of the interface that you are creating. You can have empty interface body but you must have a "{}".

An interface declaration can have two other components: the interface modifers and a list of superinterfaces.

The public access modifer indicates that the interface can be used by any class in any package. If you do not specify that your interface is public, then your interface will only be accessible to classes that are defined in the same package as the interface.

The extends clause is similar to the extends clause in a class declaration, however, an interface can extend multiple interfaces (a class can only extend one), and an interface cannot extend classes. The list of superinterfaces is a comma-delimited list of all of the interfaces extended by the new interface. An interface inherits all constants and methods from its superinterface unless the interface hides a constant with another of the same name, or redeclares a method with a new method declaration.

Interface Modifiers

The interface access modifiers include Annotation, public, protected, private, abstract, static, strictfp.

  • A compile-time error occurs if the same modifier appears more than once in an interface declaration.
  • Not all modifiers are applicable to all kinds of interface declarations. The access modifier public pertains to top level interfaces and to member interfaces.
  • If an interface type is declared public, then it may be accessed by any code, provided that the compilation unit  in which it is declared is observable. If a top level  interface type is not declared public, then it may be accessed only from within the package in which it is declared.
  • The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration.
  • The access modifier static pertains only to member interfaces.
  • Every interface is implicitly abstract. The modifier is optional in interface declarations.
  • The effect of the strictfp modifier is to make all float or double expressions within the interface declaration be explicitly FP-strict.

The following table shows the access modifiers used in the (top-level) interfaces:

Modifier Description

The interface is accessible anywhere its package is.


Every interface is implicitly abstract. The modifier is optional in interface declarations.

none (package)

A non-public interface is accessible only in its package.

strictfp The effect of the strictfp modifier is to make all float or double expressions within the interface declaration be explicitly FP-strict.

This implies that all nested types declared in the interface are implicitly strictfp.

Interface Body

The interface body contains method declarations for the methods defined within the interface. All methods are abstract methods which have no implementation. An interface can also contain constant member variables.

Method Declaration

  • The Interface methods have no implementation and a semicolon appears in place of the method body.
  • All methods of an interface are implicitly abstract and public, even if the abstract and public modifier are omitted.
  • It is an error to define a protected or private method in an interface.
  • Because interfaces can contain only abstract methods, and class methods cannot be abstract, the methods of an interface must all be instance methods.
  • An interface cannot be instantiated, so it does not define a constructor.

Field Declaration

Although a class defines data and methods that operate on that data, an interface cannot define instance fields. Fields are an implementation detail, and an interface is a pure specification without any implementation. The only fields allowed in an interface definition are constants that are declared both static and final.

  • Every field declaration in the body of an interface is implicitly public,static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
  • If the interface declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superinterfaces of the interface.
  • It is a compile-time error for the body of an interface declaration to declare two fields with the same name.
  • It is possible for an interface to inherit more than one field with the same name. Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the interface to refer to either field by its simple name will result in a compile-time error, because such a reference is ambiguous.
  • There might be several paths by which the same field declaration might be inherited from an interface. In such a situation, the field is considered to be inherited only once, and it may be referred to by its simple name without ambiguity.
  • Every field in the body of an interface must have an initialization expression, which need not be a constant expression. The variable initializer is evaluated and the assignment performed exactly once, when the interface is initialized.

A compile-time error occurs if an initialization expression for an interface field contains a reference by simple name to the same field or to another field whose declaration occurs textually later in the same interface.

For example:

interface Test {
        float f = j;
        int j = 1;
        int k = k+1;

causes two compile-time errors, because j is referred to in the initialization of f before j is declared and because the initialization of k refers to k itself.

Examples of Field Declarations

The following example illustrates some (possibly subtle) points about field declarations.

interface BaseColors {
        int RED = 1, GREEN = 2, BLUE = 4;
interface RainbowColors extends BaseColors {
        int YELLOW = 3, ORANGE = 5, INDIGO = 6, VIOLET = 7;
interface PrintColors extends BaseColors {
        int YELLOW = 8, CYAN = 16, MAGENTA = 32;
interface LotsOfColors extends RainbowColors, PrintColors {
        int FUCHSIA = 17, VERMILION = 43, CHARTREUSE = RED+90;

Ambiguous Inherited Fields : If two fields with the same name are inherited by an interface because, for example, two of its direct superinterfaces declare fields with that name, then a single ambiguous member results. Any use of this ambiguous member will result in a compile-time error.

In the above example, the interface LotsOfColors inherits two fields named YELLOW. This is all right as long as the interface does not contain any reference by simple name to the field YELLOW. (Such a reference could occur within a variable initializer for a field.)

Even if interface PrintColors were to give the value 3 to YELLOW rather than the value 8, a reference to field YELLOW within interface LotsOfColors would still be considered ambiguous.

Multiply Inherited Fields : If a single field is inherited multiple times from the same interface because, for example, both this interface and one of this interface's direct superinterfaces extend the interface that declares the field, then only a single member results. This situation does not in itself cause a compile-time error.

In the example in the previous section, the fields RED, GREEN, and BLUE are inherited by interface LotsOfColors in more than one way, through interface RainbowColors and also through interface PrintColors, but the reference to field RED in interface LotsOfColors is not considered ambiguous because only one actual declaration of the field RED is involved.

Implementing an Interface

An interface defines a protocol of behavior. A class that implements an interface adheres to the protocol defined by that interface. To declare a class that implements an interface, include an implements clause in the class declaration following the extends clause. Your class can implement more than one interface (the Java platform supports multiple inheritance for interfaces), so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

When a class implements an interface, it is essentially signing a contract. Either the class must implement all the methods declared in the interface and its superinterfaces, or the class must be declared abstract. If a class implements more than one interface, it must implement every method of each interface it implements (or be declared abstract).

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |