Implementing an Interface

Now that you know how to create interfaces, look at how they are used in developing classes. Look again at the JavaBook class that implements our Sellable interface:

public class JavaBook implements Sellable {
  public String getDescription() {
    return "Java Book";
  }

  public String getUnits() {
    return "Each";
  }

  public double getPricePerUnit() {
    return 39.95;
  }

  public double getWeight() {
    return 4.5;
  }
}

This class satisfies the contract of the interface by providing an implementation for each method. As with classes, implementing abstract methods from an interface is referred to as overriding the methods. The JavaBook class is not restricted from defining methods other than those in the Sellable interface, but it must ...

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.