The Java language provides an interface (interface) mechanism. This interface mechanism makes Java object-oriented programming more flexible. We can use interfaces to define the representation of a class, but the interface cannot contain any implementations. In the book Thinking in Java, the author describes the interface: "Interface (interface) is a step closer to the concept of abstraction (abstract). You can think of an interface as a pure abstract class. "I think the author's interpretation of the interface is no more accurate.
Understanding and good interface mechanisms will help us better grasp the object-oriented programming language of Java. Let's discuss the usage rules of the interface and the related applications.
First, the definition and implementation of the interface
Defining an interface is similar to defining a class, except to change the Class keyword to interface. Only method names, return types, and argument lists are required to define a method, and cannot have a method body. You can define fields in an interface that are dimmed as static and final, so you should first 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 (); } |
In the several interfaces defined above, flyable and talkable only define a method, and the Message has a field max_size in addition to the method. It can be seen that these interfaces only define the representation of the class, not any implementations, so they cannot be used directly. To use these interfaces, you need to have the appropriate classes to implement them. When implementing an interface, you should declare the interface to be implemented with the Implements keyword after the class name, and if you want to implement multiple interfaces, you should separate them with commas and implement the methods defined in those interfaces one at a time. As in the following 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 interface flyable to represent the ability to fly, talkable to speak, but they do not contain a specific implementation. While Parrot has both of these capabilities, we implement both the flyable and talkable interfaces for the Parrot class. We can also define a swallow (Swallow) class, but the swallow only has the ability to fly, so we just need to implement flyable for swallow. Because their respective flight methods are different, they have their own specific implementation of the flight.
In addition, because a class can implement multiple interfaces at the same time, Java's object-oriented features become very flexible. Using this feature, we can implement features such as multi-inheritance in the C + + language, and even more flexible features. Let's discuss the application of the interface in practice.
Second, use the interface to define some global variables
Because the fields inside the interface are both static and final, we can easily make use of this to create some constants. For example:
Public interface Constants { String ROOT = "/root"; int max_count = 200; int min_count = 100; } |
The constants can be referenced directly in the form of constants.root when used. We can also use this method to create a constant with an indeterminate initial value.
Public interface Randomcolor { int red = math.random () * 255; int green = Math.random () * 255; int blue = Math.random () * 255; } |
Where the values of red, green, and Blue are established the first time they are accessed, and then remain unchanged.
Iii. using interfaces to define basic data structures
In the early stages of designing a set of software systems, we can use interfaces to describe some of the characteristics of some basic data elements, and then implement them differently as needed. Please take a look at the following example:
public interface User { int Getage (); String getName (); String GetPassword (); } public class Xmluser implements user { ///Here the method in the user interface is implemented with XML technology 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.); public void AddUser (user user); public void Deluser (user user); } public class Xmluserfactory extends Userfactory { //Userfactory abstract method implemented here with XML technology } |
In this example, we define an interface user and an abstract class userfactory. Then we use XML technology to implement these two classes. As can be seen, we only need to use Userfactory getuserfactory () to get a userfactory instance, instead of considering the concrete implementation of this example. Through this example of userfactory we can also directly get user's instance, do not have to test the specific implementation method.
If we decide to implement user and userfactory using JDBC technology, we just need to implement Jdbcuser and jdbcuserfactory as described above. Then the Getuserfactory method in the Userfactory can be modified to change the way they are implemented. And we've written a call to Userfactory and the user section without any modifications.
This is a simple example of using an interface to define the data structure, and there are many flexible ways to use it in the actual application, and we need to realize it constantly in the process of learning.
Iv. Understanding the principles of distributed Applications
There are many software projects that use distributed technologies. Java has a variety of technology to support distributed applications, early use of more than the RMI, CORBA and other technologies, and now EJB technology is more popular. But these technologies, however developed, are all based on interfaces. The
takes the remote method invocation of RMI (invocation) as an example. When writing RMI applications, we need to do two basic things, first define an interface that inherits the Java.rmi.Remote interface, which should contain the method name you want to call from the far end. The next step is to 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, and the Productimpl is placed on the server side, the customer only need to use the specified rules to get the product instance on the line, not to consider how the product interface method is implemented. After defining these two classes, the Java SDK command "Rmic Productimpl" will help us to automatically generate two classes Productimpl_skel and Productimpl_stub. These two classes contain the operation mechanism of RMI invocation. Interested friends can turn these two classes back into the post-compilation study. You will find that productimpl_stub is actually an implementation class for interface Product. The RMI mechanism uses this class to generate an instance of Product for use by clients. The other class Productimpl_skel is the class that responds to Productimpl_stub's invocation request on the server side. At the bottom of the RMI communication principle is the use of ObjectInputStream and objetoutputstream through the Socket will be called by the method name and parameter list to the server side, the server end through a specific method call implementation class (in this case is Productimpl), and then pass the result back to the client via the Socket. Since the Skel and Stub classes are generated using tools, the development time is greatly reduced. In addition, if we need to modify some implementation methods or errors, only need to modify the server-side implementation class, which means that most of the maintenance of this distributed application can be done on the server side.
More and more applications now use EJB as a technology. EJB is a technology developed from RMI, which is more sophisticated than RMI and can achieve better object-oriented features. But its rules are a bit more complicated than RMI. But no matter how complex it is, it also uses interfaces to define various beans, and it also needs to write the corresponding implementation class to accomplish the specific function, and finally to communicate through the Socket. The operation mechanism of EJB has its own complexity, so the efficiency of its application will certainly be affected. Therefore, in the choice of development technology should be based on the size and characteristics of the application of careful consideration, not necessarily popular technology will certainly be able to adapt to your application. If you have a good grasp of object-oriented design principles, you can design your own. Perhaps you can design a more suitable distributed application structure according to the characteristics of your application.
v. Conclusion
In addition to some of these applications, there are many places where interfaces can be used, such as in the Java event mechanism. In addition, for some already developed systems, the structure of the larger adjustment is not very realistic, then you can define some interfaces and append the corresponding implementation to complete the expansion of the functional structure.
In a word, learning interfaces well can help us understand and apply object-oriented design principles better. So that we can design a better software system. Due to my level of limitations, if there are errors, please correct me.
Discussion on the application of interface in Java programming