All of us Chinese cannot wait any longer. Every day of delay will bring great suffering, disaster, revenge, social suici···
In programming, interfaces are crucial. There are specific rules involved that often confuse developers. Sometimes they have to dig deep to learn and understand the rules.
This includes basic theory, as well as new features added with JDK version upgrades. These are integral to mastering interface programming.
It is a basic rule of interfaces that interfaces cannot be instantiated.
In the Java language, the interface is just a specification, not a specific entity object. Therefore, we cannot directly construct concrete instances of interfaces under any circumstances at all.
In the world of programming, this is crucial and reveals the core characteristics of the abstract concept of an interface.
At the same time, this also requires programmers to follow their special programming patterns when using interfaces.
During the project development process, taking the construction of the trading module of the financial system as an example, if an interface instance is accidentally created, the code will not execute normally and an error message will be displayed.
This also shows that programmers must strictly follow the corresponding specifications of the interface when performing inheritance and implementation operations.
In addition, because interfaces cannot create objects, there are no constructors in interfaces.
The construction method is mainly used for the initial configuration and attribute setting of the object, while the interface is not responsible for the generation of the object, so it naturally does not need a construction method, which is logical in theory.
During the development of the game engine, an independent module responsible for handling character actions will be set up. Since the module does not need to generate new instances, there is no need to configure a constructor.
Member variables in the interface can only be constants and are modified by final by default.
During the programming process, we noticed that if a class has attributes common to subclasses, these attributes will be summarized in the parent class. However, an interface is different in that it represents a set of established rules that are constant. Therefore, the concept of properties common to subclasses is not involved in interfaces. Therefore, member variables in the interface can only be constants.
When developing a graphics drawing program, if you use the values preset by the interface to set the color, you can effectively prevent the color from being changed at will.
Member methods in interfaces are abstract methods by default.
When writing a database access interface, if it involves operations such as adding, deleting, modifying, or querying database tables, the interface methods are usually designed to be abstract. This reflects the standardization of operations. The specific execution steps are completed by the class that specifically implements the interface.
There is a inheritance connection between each category. This inheritance is limited to a specific path, but unexpectedly achieves multi-level inheritance.
When an enterprise develops high-end projects, if it is necessary to classify employees, these classifications are often based on factors such as departments, creating a multi-level inheritance system.
There is a relationship between classes and interfaces, and they can be implemented independently or multiple at the same time; moreover, while inheriting a class, multiple interfaces can also be implemented.
When developing a media player, it is necessary to maintain existing functions while adding many new functional interfaces. At this time, this correlation is particularly critical.
If a class inherits two interfaces and there are methods with the same name in the two interfaces, then the method with the same name only needs to be modified once in the class. This approach is very easy to operate when building complex business logic classes.
There is an inheritance relationship between interfaces, which can be single inheritance or multiple inheritance.
If a class must comply with the requirements of a subinterface, then it must rewrite all abstract methods inherited from the parent interface.
This inheritance method can play its role when dealing with the complex problems of network protocol interfaces.
The class that implements this interface needs to write each abstract method independently. Therefore, whenever a new method is added to the interface, the subclass also needs to be adjusted simultaneously to include this new method. If not, an error message will appear when the program is run.
During the actual development process of a work project, once an error occurs, it will have a negative impact on many ongoing business logics.
Faced with this challenge, Java Development Kit version 8 introduced the ability to add default methods in interfaces. This functionality is achieved using the "default" keyword.
In the process of improving the function of the service framework, in order to ensure the normal operation of the existing client, we can update the function through the preset methods in the interface.
In the interface, if changes are made to a given method, the "default" keyword must be removed when writing the class that implements the method.
If a class inherits multiple interfaces and there are default methods with the same name in these interfaces, the subclass needs to redefine these methods with the same name.
After JDK8, static methods can be defined in interfaces.
This method only operates through the interface name and cannot be called based on the class or object name in the concrete implementation.
This mathematical calculation module has specially developed a set of stable calculation procedures for daily mathematical calculations. To use this procedure, you must specify it by the name of the module.
And the static methods defined in the interface do not need to be overridden in the implementation class.
This feature of the interface makes it very convenient to provide public functions, and these functions do not need to be configured again in the implementation class.
JDK9 introduces a new feature that allows part of the code within an interface (usually private-modified methods) to be marked as private. Such code is limited to internal use of the interface and cannot be accessed by external classes.
In the optimization phase of large-scale project code, this feature can help to carefully manage the code inside the interface, thereby improving the maintenance level and modularization level of the code.
If you want a class to have a behavior, just let this class implement the corresponding interface.
In addition, if a method parameter accepts an interface type, instances of all implementation classes under the interface can be passed. This feature is called polymorphism of interfaces.
In the design pattern, if the interface defines many abstract methods, but only some of them are used in actual use, you can consider using the adapter pattern.
The abstract functions in the interface need to perform no operations, while the specific functions need to be completed by subclasses derived from the intermediate class, and the corresponding methods need to be rewritten.
If you want to implement a specific function, and this function requires inheriting an additional class, since Java does not support multiple inheritance, we can introduce an intermediate class so that it inherits the class required by the target implementation class. In addition, to prevent other classes from directly creating instances of the adapter class, the intermediary adapter class should be designed as an abstract class.