Interfaces interface
In software engineering, it is very common for a "contract" to provide the interaction between software from different development groups. Each team can independently develop its own code without knowing the other group's code. The interface in Java is such a "contract".
For example, suppose there is an intelligent vehicle in the future that can carry passengers automatically without the need for manual operation. Auto makers have developed software (Java, of course) to control the car stop, start, accelerate, turn left, and so on. Electronic navigation instrument manufacturers are responsible for developing computer systems that receive wireless transmission of GPS location data and traffic conditions, and use this information to drive cars.
Auto makers must publish industrial standard interface, which should explain in detail which methods can be used to control motor vehicles (the standard applies to any car, any manufacturer). Navigation system manufacturers can use the various methods introduced in this interface to control the car. No industrial manufacturer needs to know how other vendors implement their software. In fact, each vendor has a high degree of ownership of its own software and retains the right to modify it as long as everyone adheres to the published interface.
Interface in Java
In the Java programming language, a interface is a reference type (reference) that is similar to class and can only contain constants (constants), method signatures (methods signatures), and nested types (nested types). Interface must not contain the specific code of the method (methods body). Interface cannot be instantiated (instantiated) and can only be implemented by other classes (implemented) or inherited by other Interface.
Defining a interface is similar to creating a new class:
Copy Code code as follows:
Public interface Operatecar {
Constant declarations, if any
Method signatures
An enum with the 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 speedofcar);
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 (methods) that the interface declares.
In the example of an automated car above, the car manufacturer is the interface implementation. The Chevrolet company's approach is certainly different from the Honda approach, but they all follow the same interface. Navigation system manufacturers are the users of this interface, and their systems will drive cars based on GPS data, digital maps and traffic conditions in the direction of the car. Therefore, the navigation system will involve the following methods (methods): Turning, cutting, braking, acceleration and so on.
API interface
An example of an automated vehicle shows the application of Interface in the Industrial Standard application interface (API, application programming Interface). In business software, APIs are also common. Typically, a company sells packages that contain complex methods that other companies want to apply to their products (methods). For example, a software package that includes digital graphics processing can be sold to companies that develop terminal client image software. Once purchased, the company can apply the method defined by interface. While an image processing company exposes its APIs to all customers, the implementation of these APIs is highly confidential. In fact, the implementation of these APIs is likely to be rewritten in the future as long as the original interface are not changed.
Interfaces and multiple inheritance
In the Java programming language, interface has another important role to play. Although interface is used with a class, it is not part of a hierarchy of classes. The Java programming language does not support multiple inheritance, but interface provides alternative scenarios.
In Java, a class can only inherit from a single class, but it implements multiple interfaces. Therefore, an object can have multiple types: a type that belongs to its own class, and a type that belongs to all interfaces to which it inherits. This means that if you declare a variable to be an interface type, the variable can refer to any instance of the class that implements the interface. This section is discussed in detail in "Using Interface types".
Define a interface
An interface is defined by modifiers (modifiers), keyword interface, interface names, parent interfaces separated by commas (parent interfaces), and interface entities (interface body).
Examples are as follows:
Copy Code code as follows:
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 provides that this interface can be used by any class in any package. If you declare that the 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 inherit only one parent class, whereas an interface can inherit any number of interfaces.
Interface Entity (interface body)
An interface entity contains a declaration of all the methods it contains. Each declaration ends in quotation marks, because the interface does not implement the method it declares. All methods in an interface are public by default, so the modifier public can be omitted.
Interfaces can also declare constants. Similarly, the modifiers of the constants public, static, and final can be omitted.
Implementation of the interface
To declare that a class implements an interface, you need to use implements in the declaration of a class. Your class can implement multiple interfaces, so implements keywords can follow multiple interface names separated by commas. For convenience, implements keywords are followed by extends keywords.
An interface instance-relatable
Relatable is an interface to compare the size of two objects.
Copy Code code as follows:
Public interface Relatable {
This (object calling Islargerthan)
and other must is instances of
The same class returns 1, 0,-1
If this is greater//than, equal
To, or less than
public int Islargerthan (Relatable);
}
If you want to compare the size of two similar objects, no matter what class the object belongs to, this class needs to implement the relatable interface.
Any class can implement the relatable interface as long as there is a way to compare the relative size of the object. For a string, you can compare the number of characters; for a book, you can compare pages; for students, you can compare weight. For plane geometry objects, the comparison area is a good choice; for three-dimensional objects, it is necessary to compare volume. All of the above classes can implement the int Islargerthan () method.
If you know that a class implements the relatable interface, you can compare objects instantiated from this class.
Implementation of relatable interface
Here is a triangle class that implements the relatable interface.
Copy Code code as follows:
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;
}
}
Using interface types
When you define a new interface, you are actually defining a new reference type. You can use the interface name where you can use the data type name. If you define a reference variable of the type as an interface, the class that the variable can point to must implement the interface.
The following example is a method that returns a larger object in a pair of objects:
Copy Code code as follows:
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, the object obj1 can invoke the Islargerthan method.
Similarly, you can use the following method as long as you are implementing a relatable class.
Copy Code code as follows:
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 relatable classes that are both of the type of themselves (or of the parent class) and of the relatable type. This gives them the advantage of multiple inheritance because they can have both the behavior of the parent class and the interface.
overriding an interface
Suppose you have developed an interface called doit:
Copy Code code as follows:
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:
Copy Code code as follows:
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 go wrong because they are no longer implementing the interface correctly. All programmers using this interface will seriously protest against your changes.
You need to anticipate the needs of your interface users and design this interface well from the start. But it is often impossible to do. Another workaround is to write an interface again. For example, you can write a doitplus interface to inherit the original interface.
Copy Code code as follows:
Public interface Doitplus extends DoIt {
Boolean diditwork (int i, double x, String s);
}
Now your users can choose to continue using the old interface doit, or upgrade the new interface Doitplus.
Summarize
An interface is a communication protocol between two objects.
The declaration of an interface contains the signatures of some methods (signatures), but it does not need to be implemented, or it may contain constants.
A class that implements an interface must implement all of the methods declared by that interface.
The name of the interface can be used wherever the type name is used.
Oracle, compiling: @philhu
Several tips to prevent Java vulnerabilities
Java Exception Handling
After a bad year, Java is moving in the right direction
Seven common uses of Java enumerations
Choose Java or. NET