Showing posts with label Interface in Java. Show all posts
Showing posts with label Interface in Java. Show all posts

Rewriting Interfaces :

Consider an interface that you have developed called DoIt:


public interface DoIt {
   void doSomething(int i, double x);
   int doSomethingElse(String s);
}


Suppose that, at a later time, you want to add a third method to DoIt, so that the interface now becomes:


public interface DoIt {

   void doSomething(int i, double x);
   int doSomethingElse(String s);
   boolean didItWork(int i, double x, String s);
   
}


If you make this change, all classes that implement the old DoIt interface will break because they don't implement the interface anymore. Programmers relying on this interface will protest loudly.

Try to anticipate all uses for your interface and to specify it completely from the beginning. Given that this is often impossible, you may need to create more interfaces later. For example, you could create a DoItPlus interface that extends DoIt:


public interface DoItPlus extends DoIt {

   boolean didItWork(int i, double x, String s);
   
}
Now users of your code can choose to continue to use the old interface or to upgrade to the new interface.







  •  An interface defines a protocol of communication between two objects.
  • An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.
  • A class that implements an interface must implement all the methods declared in the interface.
  • An interface name can be used anywhere a type can be used.











Interface in Java


As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.
In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
interface Bicycle {

    //  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 bicycle, for example, such as HeroBicycle), and you'd use the implements keyword in the class declaration:
class HeroBicycle 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, 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.
 
Copyright (c) 2013 Java Discovery.