O'Reilly logo

Special Edition Using Java 2 Standard Edition by Geoff Friesen, Chuck Cavaness, Brian Keeton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required