A Java interface is a set of method declarations. It is a collection of method features. An interface has only the characteristics of the method and no method implementation. Therefore, these methods can be implemented in different places by different classes, and these implementations can have different behaviors ( Features).
Interface (English: Interface), an abstract type in the JAVA programming language, is used to require classes to implement specified methods so that objects of different classes can communicate using the same interface. An interface is usually declared with an interface. It can only contain a Method Signature and a constant declaration (variable declarations contain static and final). An interface does not contain the implementation of the method (only defined).
Interfaces cannot be instantiated, but can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an Abstract Class. In addition, in Java, interface types can be used to declare a variable, they can be a null pointer, or be bound to an object implemented by this interface.
One of the advantages of using interfaces is that they can be used to simulate multiple inheritance. Classes do not allow multiple inheritance in JAVA. All classes in JAVA must have only one parent class, and java.lang.Object (in JAVA type system) The topmost type) is the only exception.
JAVA classes can be implemented with many interfaces, whereas one interface cannot implement other interfaces.
When implementing a specific function, the interface and the implemented class are separated. The interface is defined as XxxService and is implemented as XxxServiceImpl. The interface provides a public method provider. Interfaces are used to specify the behavior of subclasses.
Benefits of interface-oriented programming:
1. According to the needs of the customer, it is proposed as an interface; the specific implementation of the business is achieved by implementing the interface class.
2. When the customer proposes a new requirement, simply write a new implementation class for the requirement business logic.
3. If this mode is adopted, the business logic is clearer, and the code is more readable, extensible, and maintainable.
4, interface and implementation separation, suitable for team collaboration development.
5, to achieve a loosely coupled system, easy to upgrade and expand in the future.
In the Java language specification, the characteristics of a method include only the name of the method, the number and type of parameters, and not the return type of the method, the name of the parameter, and the exception thrown. When the Java compiler checks for overloads of methods, it is determined based on these conditions whether the two methods are overloaded. However, when the Java compiler checks for permutation of methods, it will further check whether the return types of the two methods (the subtypes and subtypes) are the same as the thrown exceptions.
The Java interface itself has no implementation, because the Java interface does not refer to the representation, but only describes the public behavior, so the Java interface is more abstract than the Java abstract class. But the interface is not a class, you can't instantiate an interface with the new operator.
Such as:
x=new comparable(...);//This is the error.
But you can declare interface variables
Comparable x; //This is allowed.
Java interface methods can only be abstract and public. Java interfaces cannot have constructors. Java interfaces can have public, static, and final properties. That is, the attributes in the interface can be defined as
Public static final int value=5;
The interface separates the features of the method from the implementation of the method. This segmentation is reflected in the fact that the interface often represents a role that wraps the operations and properties associated with the role, and the class that implements this interface is the actor who plays the role. A character is played by a different actor, and different actors do not require a commonality in addition to playing a common role.
The interface is defined as followsThe body of the interface contains abstract methods, but all methods are abstract in the interface (the definition), so the abstract keyword is not needed in the interface. Since the interface represents a collection of external behaviors, any method is public (open) within the interface.
The members in the interface are static, final, and public. Otherwise, they can be any type of class or interface.
The use of interfaces solves the problem that a class can only inherit from a single class because a class can implement multiple interfaces.
So how do you implement an interface in Java and call methods in the interface in implementing its subclasses?
Interface implementationIf a class implements an interface and does not implement all the methods of the interface, it must be marked as abstract (abstract class). A subclass of an abstract class must implement its unfinished method. If the subclass still does not implement all the methods of the interface, then the subclass still needs to be marked as abstract.
Interfaces are commonly used in the Java programming language and are used to make callback functions [2]. Java does not allow methods to be passed as arguments. Therefore, one solution is to define an interface and treat the interface as a method argument to use the object's method signature.
Subinterface [edit]
An interface can be extended to several different interfaces, and the methods described above can be used, for example:
The above program fragment is a legally defined sub-interface. Unlike the class, the interface allows multiple inheritance, and Predator and Venomous may define or inherit the same method, such as kill (Prey prey), when a class implements VenomousPredator. It will implement both methods at the same time.
Some general Java interfaces are available for reference:
Comparable
Have a method compareTo that describes whether two objects are equal, or one of them is larger than the other. Generics allow classes that have already been implemented, and their objects can be used to compare with each other.
Serializable is a marker interface without any interface or field, only an empty body, which is used to indicate that a class can be serialized. Its Javadoc describes how it works and does not need to be forced to program.
In order to be able to transform up to multiple base types. That is, by using multiple implementations of the interface, it can be transformed into multiple interface base types.
Use of java interfaceJava interfaces or abstract classes can use up-conversion, they use java runtime polymorphism technology, or runtime binding technology. The following is a common use of a java interface:
result:
I'm a student.
I'm a teacher.
F5000 High Accuracy Smart Gas Pressure Controller
Smart Gas Pressure Controller,Smart Pressure Controller,Digital Pressure Calibrator,High Precision Pressure Equipment
Shenzhen Ever-smart Sensor Technology Co., LTD , https://www.fluhandy.com