An interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, appear as follows:
interface Bicycle {
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
To implement this interface, the name of your class would change (to TTCBicycle, for example), and you'd use the implements keyword in the class declaration:
class TTCBicycle implements Bicycle {
// remainder of this class implemented as before
}
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
No comments:
Post a Comment