Program 7.12: The Import Interface

Interfaces are the next level of abstraction. An interface is like a class with nothing but abstract methods and final, static member variables. All methods and fields of an interface must be public.

However, unlike a class, an interface can be added to a class that is already a subclass of another class. Furthermore an interface can apply to members of many different classes. For instance you can define an Import interface with the single method calculate_tariff().

public interface Import {

  public abstract double calculate_tariff();

You might want to use this interface on many different classes, cars among them but also for clothes, food, electronics and more. It would be inconvenient to make all these objects derive from a single class. Furthermore each different type of item is likely to have a different means of calculating the tariff. Therefore you define an Import interface and declare that each class implements Import.

Copyright 1996 Elliotte Rusty Harold
This Chapter