How to Implementing Interfaces in java?
To actually utilize this interface you create a class that involves a public double calculateTariff() method and declare in which the class implements Import. For example 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 benefits of interfaces over classes is in which a single class may implement more than one interface. For instance, 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 in which only add a type to a class, but do not declare any additional methods.