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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.