Home > DeveloperSection > Blogs > Interface E.g.

Interface E.g.


Java OOPS 
Ratings:
2 Comment(s)
 579  View(s)
Rate this:
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.


Interfaces

By Mayank Tripathi on   one year ago
Ankita, you must explain why we need to use interfaces in our code, what is the need of interface implementation

Interface

By ANkita gupta on   one year ago
There are several reasons, an application developer needs an interface, one of them is Java's feature to provide multiple inheritance at interface level. It allows you to write flexible code, which can adapt to handle future requirements. Some of the concrete reasons, why you need interface is :

1) If you only implement methods in subclasses, the callers will not be able to call them via the interface (not common point where they are defined).
 2) Interfaces are a way to declare a contract for implementing classes to fulfil; it's the primary tool to create abstraction and decoupled designs between consumers and producers.
3) Because of multiple inheritance, interface allows you to treat one thing differently. For example a class can be treated as Canvas during drawing and EventListener during event processing. Without interface, it's not possible for a class to behave like two different entity at two different situations. Here is an example of how interface supports multiple inheritance in Java.
interface Canvas{
  public void paint(Graphics g);
}

interface EventListener{
  public boolean process(Event e);
}

pubic class Game implements Canvas, EventListener{

 @Override
 public void paint(Graphics g){
   g.drawLine(Color.RED);
 }

 @Override
 public boolean process(Event e){
   KeyCode code =  e.getKeyPressed().getCode();
 }

}
4) "Programming to interface than implementation" is one of the popular Object oriented design principle, and use of interface promotes this. A code written on interface is much more flexible than the one which is written on implementation.

5) Use of interface allows you to supply a new implementation, which could be more robust, more performance in later stage of your development.

In short main use of interface is to facilitate polymorphism. interface allows a class to behave like multiple types, which is not possible without multiple inheritance of class. It also ensures that you follow programming to interface than implementation pattern, which eventually adds lot of flexibility in your system.


Don't want to miss updates? Please click the below button!

Follow MindStick