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

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

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