1. What is interface?
Objects must expose methods to deal with the external world. These methods constitute the interface of the object. For example, the button in front of a TV is an interface for people to deal with TV. You can press the power button to control the TV to turn on or off.
In most cases, an interface is a set of unimplemented methods (methods ). Take the method of cycling as an example. If an interface is defined, it may be as follows:
Interface bicycle {<br/> void changecadence (INT newvalue); // change frequency <br/> void changegear (INT newvalue ); // Change the gear ratio <br/> void speedup (INT increment); // accelerate <br/> void applybrakes (INT decrement ); // brake <br/>}< br/>
To implement the above interface, you must first define a class (define a bicycle of a specific brand, such as acmebicycle) and use the Java keyword implements when defining the class.
Class acmebicycle implements bicycle {<br/> // remainder of this class implemented as before <br/>}< br/>
Implementing an interface makes others feel that the methods provided by this class are more formal. This interface forms a contract of a class dealing with the external world. This contract will take effect during code compilation. If this interface is used in the defined class, you need to implement all the methods defined in the interface so that no errors will occur during compilation.
Note: To compile the acmebicycle class, you must add the public keyword before the implementation method.
Address: http://download.oracle.com/javase/tutorial/java/concepts/interface.html