Defining an Interface

You define an interface using syntax very similar to that of a class definition. However, there are a few exceptions. Namely, an interface cannot define any implementation for its methods and its fields are always public, static, and final.

An example interface is shown in Listing 9.2. It is followed by a set of classes declared to implement the interface in Listing 9.3 through Listing 9.5.

Code Listing 9.2. Sellable.java—Sellable Product Interface
public interface Sellable {
  String getDescription();
  String getUnits();
  double getPricePerUnit();
  double getWeight();
}
Code Listing 9.3. JavaBook.java—Programming Language Book Class
 public class JavaBook implements Sellable { public String getDescription() { return "Java ...

Get Special Edition Using Java 2 Standard Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.