---
title: "Interface E.g."  
description: "interface Car {       // wheel revolutions per minute       void changeCadence(int newValue);       void changeGear(int newValue);       void spee"  
author: "ANkita gupta"  
published: 2015-05-02  
updated: 2018-02-23  
canonical: https://www.mindstick.com/blog/10877/interface-e-g  
category: "java"  
tags: ["oops"]  
reading_time: 2 minutes  

---

# Interface E.g.

```
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](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) it [promises](https://www.mindstick.com/forum/160100/how-do-promises-in-javascript-simplify-asynchronous-code-compared-to-callbacks) to provide. [Interfaces](https://www.mindstick.com/articles/12100/interfaces-in-java-real-life-example) form a [contract](https://www.mindstick.com/blog/212/wcf-contracts) between the class and the outside world, and this contract is enforced at build time by the [compiler](https://www.mindstick.com/articles/27/just-in-time-compiler). [If your](https://www.mindstick.com/forum/156103/what-will-be-your-approach-if-your-seo-method-doesn-t-work) class claims to [implement an interface](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression), all [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) defined by that interface must appear in its [source code](https://www.mindstick.com/forum/33476/pls-send-me-source-code-for-changing-passward-in-asp-dot-net-i-tried-so-much-from-google-but-its-not-workining) before the class will successfully compile.

---

Original Source: https://www.mindstick.com/blog/10877/interface-e-g

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
