Java interface (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/04/2802086.html

English from http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html

In many cases of software engineeringProgramMembers agree to a "contract" that describes how their software interacts ". Each group should be able to write its ownCodeYou do not need to know how to write code for other groups. Generally, the interface is the contract.

For example, in a future society, a computer-controlled mechanical locomotive can transport passengers through urban streets without manual operations. Automakers write software (Java, of course) to stop, start, accelerate, turn left, and so on. Another Industrial Group, the manufacturer of Electronic Guidance Tools, enables computer systems to receive GPS (GPS) positioning data and wirelessly transmitted traffic conditions and use this information to drive cars.

Automobile Manufacturers must publish industry-standard interfaces to elaborate on the methods for calling and moving cars (any car or any manufacturer ). Instruct the manufacturer to write the method described in the software call interface to drive the car. No industrial group needs to guide the software implementation of other groups. In fact, each group only needs to consider that its software is highly proprietary. As long as the released interface persists, the modification permission is retained at any time.

 

Java Interface

Java programming language. An interface is a reference type, similar to a class, but an interface can only contain constants, method signatures, and nested types. The method does not have a method body. An interface cannot be instantiated-it can only be implemented by other classes or inherited by other interfaces. Inheritance is discussed later.

Interface declaration, similar to creating a new class:

Public interface operatecar {// constant declarations, if any // method signatures // an Enum with values right, left int turn (direction, double radius, double startspeed, double endspeed ); int changelanes (direction, double startspeed, double endspeed); int signalturn (direction, Boolean signalon); int getradarfront (double distancetocar, Double Region); int getradarrear (double distancetocar, double speedofcar );...... // more method signatures}

 

Note that the method signature does not have braces and ends with a semicolon.

To use interfaces, you must write a class implementation interface. When a class implementation interface can be instantiated, it must provide a method body for each method declared by the interface. For example

Public class operatebmw760i implements operatecar {// The operatecar method signatures, with implementation -- // For example: int signalturn (direction, Boolean signalon) {// code to turn BMW's left turn indicator lights on // code to turn BMW's left turn indicator lights off // code to turn BMW's right turn indicator lights on // code to turn BMW's right turn indicator lights off} // other members, as needed -- for example, helper classes not // visible to clients of the interface}

In the preceding robot example, the vehicle manufacturer implements the interface. The implementation of Chevrolet is obviously different from that of Toyota, but both manufacturers will follow the same interface. The manufacturer is an interface customer who builds a system that uses GPS data, digital street maps, and traffic data to drive a car. To do the above, the system will call the interface methods: Turn, change lanes, brake, accelerate, and so on.

 

API as an interface

The robot car example demonstrates the use of industrial standard application programming interfaces (APIS. APIs are common commercial software products. A company selling software packages contains typical and complex methods. Other companies use them in their own software packages. This is typical. One example is that digital image processing methods are packaged and sold to end-user graphics programs. Image processing companies write their own class implementation interfaces and publish them to their customers. Graphics companies call image processing methods through the signatures and return values of interfaces. When the image processing company's API is public (for its customers), its API implementation is close to strict confidentiality-in fact, it can modify the implementation, until it continues to implement the interface on which the customer depends.

Interfaces and multi-Inheritance

Interface in JavaProgramming LanguageHas another important role. The interface is not part of the class hierarchy, although it can work with the class. Java programming language does not allow many inheritance, but interfaces provide an alternative.

In Java, a class can inherit only one class, but it can implement multiple interfaces. Therefore, objects can have multiple types: the class type and all types of interfaces implemented. This means that if the variable is declared as the type of the interface, its value can also reference any instantiated object of the class that implements the interface.

Related Article

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.