blog

Home / DeveloperSection / Blogs / Interface E.g.

Interface E.g.

ANkita gupta 2228 02-May-2015
interface Car {

    // wheel revolutions per minute
    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 a particular brand of Car, for example, BMWCar), and you'd use the implements keyword in the class declaration: 

class BMWCar implements Car {

    int cadence = 0;
    int speed = 0;
    int gear = 1;
   // The compiler will now require that methods
   // changeCadence, changeGear, speedUp, and applyBrakes
   // all be implemented. Compilation will fail if those
   // methods are missing from this class.
    void changeCadence(int newValue) {
         cadence = newValue;
    }
    void changeGear(int newValue) {
         gear = newValue;
    }
    void speedUp(int increment) {
         speed = speed + increment;
    }
    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }
    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" +
             speed + " gear:" + gear);
    }
}

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, and this contract is enforced at build time by the compiler. 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.


Updated 23-Feb-2018

Leave Comment

Comments

Liked By