Implementing Interfaces

To actually use this interface you create a class that includes a public double calculateTariff() method and declare that the class implements Import. For instance here's one such class:

public class Car extends MotorVehicle implements Import {

  int numWheels = 4;

  public double calculateTariff() {
    return this.price * 0.1;
  }

}

One of the advantages of interfaces over classes is that a single class may implement more than one interface. For example, this Car class implements three interfaces: Import, Serializable, and Cloneable

import java.io.*;


public class Car extends MotorVehicle 
 implements Import, Serializable, Cloneable {

  int numWheels = 4;

  public double calculateTariff() {
    return this.price * 0.1;
  }

}

Serializable and Cloneable are marker interfaces from the class library that only add a type to a class, but do not declare any additional methods.


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 25, 1998