J2EEOnline J2EE   





Java Packages and Interfaces  «Prev  Next»
Lesson 6Inside interfaces
ObjectiveCreate Interfaces to define General Characteristics
Interfaces are Java constructs similar to classes that do not contain any functioning code. Interfaces are used to define the general design for a class without getting into the details of an implementation.
The idea behind interfaces is that you can use them to define the general make-up of a type of class and then apply it to create the classes themselves. Following is an example of a simple interface:
interface Fish {
  public void swim();
  public void eat();
}
Notice that the methods defined in the Fish interface are simply declarations and contain no actual code.
To create a class based on an interface, you must use the implements keyword, like this:
class Shark implements Fish {
  boolean isHungry;

  public void swim() {
    isHungry = true;
  }

  public void eat() {
    isHungry = false;
  }
}


Since the Shark class implements the Fish interface, it must provide implementations of the swim() and eat() methods.
You could also create other types of fish based on the Fish interface, in which case they would provide suitable implementations of the swim() and eat() methods.
The benefit of implementing an interface in this situation is that all fish classes would have commonly supported methods, as defined by the Fish interface.
Although interfaces primarily consist of method declarations, it is possible to declare member variables in them as well. However, any member variable declared in an interface is implicitly made static and final.
The following link discusses a class that implements two interfaces where the variables and methods of the interface are the same.
  1. Interface Ambiguous Fields
  2. Ambiguous Interface with Exceptions


Rules for Interfaces in Java.
  1. Every interface is implicitly abstract. This modifier is obsolete for interfaces and should not be used in new Java programs.
  2. An interface can extend 1 or more interfaces.
  3. Every field declaration in the body of an interface is implicitly 1) public, 2) static and 3) final. It is permitted, but strongly discouraged as a matter of style, to redundantly specify any or all of these modifiers for such fields.
    A constant declaration in an interface must not include any of the modifiers synchronized, transient or volatile, or a compile-time error occurs.
  4. It is possible for an interface to inherit more than one field with the same name. Such a situation does not 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.
  5. Every method declaration in the body of an interface is implicitly public and abstract, so its body is always represented by a semicolon, not a block.
  6. A method in an interface cannot be declared static, because in Java static methods cannot be abstract.
  7. A method in an interface cannot be declared native or synchronized, or a compile-time error occurs. These keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared native or synchronized in a class that implements the interface.
Java Interfaces - Exercise
Click the Exercise link below to create an interface.
Java Interfaces - Exercise