The Java language provides an interface mechanism. This interface makes Java object-oriented programming more flexible. We can use interfaces to define the expression of a class, but interfaces cannot contain any implementations. In the book thinking in Java, the author describes interfaces as follows: "interfaces are more advanced than abstract concepts. You can regard an interface as a pure abstract class ." I think the author's explanation of the interface is no more accurate.
Understanding and using the interface mechanism will help us better understand the Object-Oriented JavaProgramming Language. Next we will discuss the Interface Usage rules and related applications.
I. interface definition and implementation
The definition interface is similar to the definition class, but the class keyword must be replaced with the interface. When defining a method, you only need the method name, return type and parameter list, and the method body is not allowed. Fields can be defined in the interface. All these fields are implicitly static and final. Therefore, set the values of these fields as needed. For example:
Public interface flyable {void fly ();} Public interface talkable {void talk ();} Public interface message { Int max_size = 4096; String getmessage (); } |
Among the interfaces defined above, flyable and talkable only define one method, while the message contains a field max_size in addition to the method. It can be seen that these interfaces only define the class representation, without any implementation, so they cannot be used directly. To use these interfaces, you must have corresponding classes to implement them. When implementing interfaces, you should first declare the interfaces to be implemented with the implements keyword after the class name. If you want to implement multiple interfaces, separate them with commas, then implement the methods defined in these interfaces one by one. Example:
public class parrot implements flyable, talkable { Public void fly () { system. out. println ("flying like a parrot... "); } Public void talk () { system. out. println ("Hello! I am a parrot! "); } public class textmessage implements message {string message; Public void setmessage (string MSG) {message = MSG; If (message. length ()> max_size) message = message. substring (0, max_size) ;} Public String getmessage () {return message; } |
In the parrot (parrot) example, we use the flyable interface to represent the flight capability, while talkable indicates the speaking capability, but they do not include the specific implementation. The parrot has both of these capabilities, so we have implemented the flyable and talkable interfaces for the parrot class at the same time. We can also define a swallow class, but the swallow only has the flight capability, so we only need to implement flyable for swallow. Because their flight methods are different, they have their own specific flight implementations.
In addition, because a class can implement multiple interfaces at the same time, Java's object-oriented features become very flexible. With this feature, we can implement features similar to that inherited in C ++, and even more flexible features. Next we will discuss the application of interfaces in practice.
2. Use interfaces to define some global variables
Because the fields in the interface are both static and final, we can easily use this to create some constants. For example:
Public interface const ants {string root = "/root"; int max_count = 200; int min_count = 100 ;} |
Constants can be referenced directly in the form of constants. Root. We can also use the following method to create constants with Unknown initial values.
Public interface randomcolor {int Red = math. Random () * 255; int Green = math. Random () * 255; int Blue = math. Random () * 255 ;}
Here, the values of red, green, and blue are created when they are accessed for the first time and remain unchanged.
3. Use interfaces to define basic data structures
In the initial stage of designing a software system, we can use interfaces to describe the features of some basic data elements, and then implement them as needed. Take a look at the following example:
Public interface user {int getage (); string getname (); string GetPassword ();} public class xmluser Implements user {// here, the XML technology is used to implement the public int getage (){... }public string getname () { ... }public string GetPassword () { ...} } public abstract class userfactory { Public static userfactory getuserfactory () { return New xmluserfactory (); } public user getuser (string name); public user GetAdmin (); public user createuser (string name, string password, int age); Public void adduser (User user ); Public void deluser (User user ); } public class xmluserfactory extends userfactory {// userfactory abstract method implemented by XML technology / TD> |
In this example, we define an interface user and an abstract class userfactory. Then we use XML technology to implement these two classes. We can see that we only need to use getuserfactory () of userfactory to get an instance of userfactory, instead of considering the specific implementation method of this instance. Through the userfactory instance, we can also directly obtain the user instance, and do not need to test the specific implementation method.
If we decide to use JDBC technology to implement user and userfactory, we only need to implement jdbcuser and jdbcuserfactory in the above form. Modify the getuserfactory method in userfactory to change its implementation method. However, we do not need to make any changes to the part that has been written to call userfactory and user.
This is a simple example of using interfaces to define the data structure. There are still many flexible usage methods in practical applications. You need to continue to understand this in the learning process.
4. Understand the principles of distributed applications
Currently, many software projects use distributed technologies. Java has a variety of technologies that support distributed applications. In the early days, many technologies were used, such as RMI and CORBA. Currently, EJB technology is more popular. However, no matter how the technology develops, it is actually based on interfaces.
Take remote method call RMI (Remote Method Invocation) as an example. When writing RMI applications, we need to do two basic things. First, we need to define an interface that inherits Java. RMI. remote interface, which should contain the name of the method you want to call from the remote end. Next, write a class to implement the methods in this interface. For example:
Public interface product extends Java. RMI. remote { string getname () throws Java. RMI. remoteException; } public class productimpl implements product { string name; Public productimpl (string N) {name = n ;} Public String getname () throws Java. RMI. remoteException {return name; }} |
In this example, the interface product is placed on the client, while the productimpl is placed on the server. The customer only needs to use the specified rules to obtain the product instance, you do not need to consider how the methods in the product interface are implemented. After defining these two classes, you can use the Java Development Kit command "rmic productimpl" to automatically generate two classes: productimpl_skel and productimpl_stub. These two classes contain the operating mechanism of RMI calls. If you are interested, you can decompile these two classes and study them. You will find that productimpl_stub is actually an implementation class of the interface product. The RMI mechanism uses this class to generate the product instance for the client to use. The other productimpl_skel class is the class that responds to the call request of productimpl_stub on the server side. The underlying communication principle of RMI is to use objectinputstream and objetoutputstream to transmit the method name and parameter list to be called through the socket to the server, the server then calls the corresponding method of the implementation class (productimpl in this example) through a specific method, and then returns the result to the client through socket. Because skel and stub classes are generated by tools, the development time is greatly reduced. In addition, if we need to modify some implementation methods or errors, we only need to modify the server implementation class. That is to say, most of the maintenance work of such distributed applications can be completed on the server.
Nowadays, more and more applications use the EJB technology. EJB is a technology developed from RMI. It is better defined than RMI and can get better object-oriented features. But its rules are more complex than RMI. However, no matter how complicated it is, it also uses interfaces to define different beans, and also needs to write corresponding implementation classes to complete specific functions, finally, we need to use socket for communication. The Operational Mechanism of EJB has some complexity, so the efficiency of its application will naturally be affected. Therefore, when selecting development technology, you should carefully consider the scale and characteristics of the application, and not necessarily popular technologies will be able to adapt to your application. If you have mastered the object-oriented design principles, you can design them on your own. Maybe you can design a more suitable distributed application structure based on the characteristics of your application.