In software engineering, it is very common to define how software from different development groups interact with each other in a "contract. Each group can develop its own code independently without knowing the code of another group. The interface in Java is such a "contract ". For example, if there is a smart car in the future society, it can automatically carry passengers without manual operation. Auto manufacturers have developed software (Java, of course) to control such cars, such as stopping, starting, accelerating, and turning left. The manufacturer of electronic navigation instruments is responsible for developing computer systems that receive wireless GPS location data and traffic conditions, and applying such information to drive cars. Automobile Manufacturers must publish the industrial standard interface, which must explain in detail which methods can be used to control automobile movements (This standard applies to any automobile and any manufacturer ). The navigation system manufacturer can use the various methods described in this interface to control vehicles. No industrial vendor needs to know how other vendors implement their software. In fact, as long as everyone strictly abides by the published interface, each vendor has a high degree of ownership of its own software and has the right to modify it at any time. In java, an interface is a reference type in java programming language. It is similar to a class, so it can only contain constants and method signatures) and nested types (nested types ). The Interface must not contain the method body ). An Interface cannot be instantiated. It can only be implemented by other classes or inherited by other interfaces. Defining An interface is similar to creating a new class: public interface OperateCar {// constant declarations, if any // method signatures // An enum with values RIGHT, LEFT int turn (Direction direction, double radius, double startSpeed, double endSpeed); int changeLanes (Direction direction, double startSpeed, double endSpeed); int signalTurn (Direction direction, boolean signalOn); int getRadarFront (double distanceToCar, double spe EdOfCar); int getRadarRear (double distanceToCar, double speedOfCar );...... // More method signatures} to use the above interface, you need to write a class to implement it. When an instantiated class implements an interface, it needs to provide specific code for all the methods declared by the interface (methods. In the preceding example, the automobile manufacturer is the real-time provider of the interface. The methods implemented by Chevrolet are certainly different from Honda's methods, but they all follow the same interface. The navigation system manufacturer is the user of this interface. Their system will drive the car Based on GPS data, digital maps, and traffic conditions of the car. Therefore, this navigation system will involve the following methods (methods): Turning, cutting, braking, acceleration, and so on. The example of API interface automation automobile shows the Application of Interface in industrial standard Application interface (API, Application Programming Interface), which is also common in commercial software. Generally, a software package released by a company contains a complex method (methods) that other companies want to apply to their own products ). For example, a software package containing a digital image processing method can be sold to a company that develops client image software. After the purchase, the company can apply the method defined by interface. While image processing companies disclose their APIs to all customers, the implementation of these Apis is highly confidential. In fact, as long as the original interface remains unchanged, the implementation methods of these APIs are likely to be overwritten in the future. Interfaces and multi-inheritance play another important role in java programming languages. Although an Interface is used with a class, it is not part of the class hierarchy. The java programming language does not support multiple inheritance, but the interface provides an alternative. In java, a class can only inherit from a single class, but it can implement multiple interfaces. Therefore, an object can have multiple types: The type of its own class, and the type of all interfaces It inherits. This means that if you declare a variable as an interface type, this variable can refer to any instance of the class that implements this interface. This section details how to use the interface type. Define an interface the definition of an interface is defined by the modifiers, the keyword interface, the interface name, the parent interface (parent interfaces) separated by commas ), and interface body ). Example: public interface GroupedInterface extends Interface1, Interface2, Interface3 {// constant declarations // base of natural logarithms double E = 2.718282; // method signatures void doSomething (int I, double x); int doSomethingElse (String s);} Public specifies that this interface can be used by any class in any package. If you declare that this interface is public, it can only be used by classes in the same package. An interface can inherit other interfaces, just as a class can inherit other classes. However, a class can only inherit one parent class, but an interface can inherit any number of interfaces. The interface body contains the declaration of all the methods it contains. Each declaration ends with a quotation mark, because the interface does not need to implement the method it declares. All methods in the interface are public by default, so the modifier public can be omitted. The API can also declare constants. Similarly, the modifier public, static, and final of constants can be omitted. To declare a class to implement an interface, you need to use implements in the class declaration. Your class can implement multiple interfaces, so the implements keyword can follow multiple interface names separated by commas. For convenience, the implements keyword is mostly behind the extends keyword. An interface instance-RelatableRelatable is an interface used to compare the sizes of two objects. Public interface Relatable {// this (object calling isLargerThan) // and other must be instances of // the same class returns 1, 0, -1 // if this is greater // than, equal // to, or less than other public int isLargerThan (Relatable other );} if you want to compare the sizes of two similar objects, No matter what class the object belongs to, this class needs to implement the Relatable interface. As long as there is a way to compare the relative size of the object, any class can implement the Relatable interface. For strings, the number of characters can be compared; for books, the number of pages can be compared; for students, the weight can be compared. Compared area is a good choice for plane geometric objects. for three-dimensional objects, it is necessary to compare the volume. All the above classes can implement the int isLargerThan () method. If you know that a class implements the Relatable interface, you can compare the objects instantiated from this class. The implementation of the Relatable interface is a triangle class, which implements the Relatable interface. Public class RectanglePlus implements Relatable {public int width = 0; public int height = 0; public Point origin; // four constructors public RectanglePlus () {origin = new Point (0, 0);} public RectanglePlus (Point p) {origin = p;} public RectanglePlus (int w, int h) {origin = new Point (0, 0); width = w; height = h;} public RectanglePlus (Point p, int w, int h) {origin = p; width = w; height = H;} // a method for moving the rectangle public void move (int x, int y) {origin. x = x; origin. y = y;} // a method for computing // the area of the rectangle public int getArea () {return width * height ;} // a method required to implement // the Relatable interface public int isLargerThan (Relatable other) {RectanglePlus otherRect = (RectanglePlus) other; if (this. getArea () <otherRect. getArea () Return-1; else if (this. getArea ()> otherRect. getArea () return 1; else return 0 ;}} when you define a new interface using the interface type, you are actually defining a new reference type. You can use the interface name wherever you can use the data type name. If you define a reference variable of the interface type, the class of the object to which the variable points must implement the interface. The following example shows a method for returning large objects in a pair of objects: public Object findLargest (Object object1, Object object2) {Relatable obj1 = (Relatable) object1; Relatable obj2 = (Relatable) object2; if (obj1 ). isLargerThan (obj2)> 0) return object1; else return object2;} by converting the Data Type object1 to Relatable, object obj1 can call the isLargerThan method. Similarly, the following method can be used as long as the Relatable class is implemented. Public Object findSmallest (Object object1, Object object2) {Relatable obj1 = (Relatable) object1; Relatable obj2 = (Relatable) object2; if (obj1 ). isLargerThan (obj2) <0) return object1; else return object2;} public boolean isEqual (Object object1, Object object2) {Relatable obj1 = (Relatable) object1; Relatable obj2 = (Relatable) object2; if (obj1 ). isLargerThan (obj2) = 0) return true; else return False;} These methods apply to any "Relatable" class, regardless of their inheritance relationships. Implements the Relatable class, which belongs to both the own (or parent class) type and the Relatable type. This gives them the advantages of multi-inheritance, because they can have both parent class and interface behavior. Rewrite interface suppose you have developed an interface named DoIt: public interface DoIt {void doSomething (int I, double x); int doSomethingElse (String s);} then, you want to add a new method in this interface, so the code becomes: public interface DoIt {void doSomething (int I, double x); int doSomethingElse (String s ); boolean didItWork (int I, double x, String s);} If you modify this, all classes that implement the old DoIt interface will encounter errors, because they no longer implement this interface correctly. All programmers who use this interface will seriously protest against your modifications. You need to estimate the needs of your interface users and complete the design of this interface from the very beginning. But this is often not possible. Another solution is to write another interface. For example, you can write a DoItPlus interface to inherit the original interface. Public interface DoItPlus extends DoIt {boolean didItWork (int I, double x, String s);} Now you can choose to continue using the old interface DoIt, or upgrade the new interface DoItPlus.