What is interface?

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.