1. Why interfaces are used
The interfaces in Java are a set of requirements descriptions. For example, the following is the definition of the Comparable<t> interface:
Public Interface Comparable<t> { int compareTo (T o);}
A CompareTo method is defined in the Comparable<t> interface, which is the requirement that it describes. If we want to call the Arrays.sort method to compare a People object array, then the people object must be "comparable", that is, the people class needs to implement the Comparable<t> interface. An implementation class for an interface needs to implement the methods defined in the interface. That is, the interface describes a set of requirements, and a class that implements an interface needs to implement the requirements described by this interface. For example, we want the people class object to be comparable, so we can:
Public class Implements comparable<people> { ... Public int CompareTo (People p) { // define specific comparison criteria } ...}
In fact, the Arrays.sort method requires the object it compares to implement the comparable interface because it is the CompareTo method of the object that it calls when it compares the object (because it does not know the criteria for evaluating an object size, which is determined by us). So why don't we define a CompareTo instance method directly in the people class to define the comparison criteria for people objects, but rather to implement a comparable<t> interface? Let's say, for example, that if we call the following code to compare the People object array peoples:
Arrays.sort (peoples);
Inside the sort method, a code similar to the following is actually called to compare the people object:
if (Peoples[i].compareto (peoples[j]) > 0) { // If true means peoples[i] greater than peoples[j]}
That is, the Arrays.sort method internally invokes the CompareTo method of the People object, so how does the compiler know that a method is actually defined in the People class? If the people class does not implement the comparable interface, the compiler will have to check whether the class implements this method, which will undoubtedly increase the overhead, if there is more than one method in the interface, the overhead is even greater. And there may be more than one object to compare, if there is a Date object later, the job object needs to be compared with us? If each call to the corresponding object of the CompareTo method to check the following it does not implement this method, will virtually add a lot of unnecessary overhead.
In turn, let's look at the benefits of introducing interfaces. The "Implements Comparable<people>" in the head of the people class is like telling the compiler "I am comparable and can call my CompareTo method directly without checking if I have this method". In this way, all comparable objects, as long as the Comparable<t> interface is implemented, the compiler will know that it must define the CompareTo method. An interface actually describes a specification that shows only what needs to be implemented by this interface, and does not enforce how the requirement is implemented. Just like the comparable<t> interface we mentioned before, we need to compare the size of the object not only people object one, since everyone needs to compare size, then simply to a rule, all want to compare the size of the object's class are implemented Compareble<t> interface , unifying the requirements for what the comparable objects should meet (the demand here is the CompareTo method).
2. Interface features now that you know why to use interfaces, let's look at what the interface is all about. First, the access modifier for the interface itself can be public or protected, and all methods in the interface can be public only (so it is not possible to add this modifier). In the second interface, constants can be defined, but cannot contain instance variables (the implementation class that provides an instance variable that the activity should give to the interface). The steps to use the interface are also simple, as we mentioned above, by simply using the key sub-implements with the interface name to indicate that the class is implementing this interface, and then defining the method that the interface requires to implement in the definition of the class. Interface is just a specification, so we cannot instantiate an interface with new. But we can declare a variable of the interface type, and the interface variable can only refer to the class object that implements the interface, such as the following code:
New People (...);
Although the interface is not a class, we can also use instanceof to determine whether an object implements an interface:
if instanceof comparable) { ...}
We can also "inherit" an interface:
Public Interface extends InB { ...}
Interface also has a very useful feature is that a class can implement multiple interfaces. In fact, the interface features mentioned above as long as the "interface is only a specification" the essence of the very good understanding: such as interface can not be instantiated, cannot contain instance variables, because the interface is different from the class is a "blueprint", interface more like a "label." 3. Interface vs. abstract class an essential difference between interfaces and abstract classes is that abstract classes are simply classes that contain abstract methods. In other words, abstract classes are further "abstractions" of the regular classes. A regular class can be instantiated directly, because the methods it defines are explicitly implemented, meaning that the behavior of the corresponding class object should be fully determined. Abstract classes contain abstract methods, which means that part of an object's behavior is not fully determined. Abstract methods can also be understood as a requirement description, because the abstract class exists "needs to be solved", so it can not be instantiated, can only derive the subclass, solve the requirement (that is, the implementation of the abstract method), so that the whole "blueprint" of the instance is determined to be able to instantiate. For example, abstract class people defines a write abstract method, because writing this action different people can choose different ways to complete (for example, some people are left-handed, take a pen posture is different). Because an abstract class is a class, a class can inherit only one abstract class, not "multiple inheritance" as an interface. Abstract classes, on the other hand, are inherently a class, so they can contain instance variables, or they can include implementations of methods. An interface can contain only public methods, and constants in the interface are publicly (because it is "transparent" as a specification), whereas abstract classes can contain private variables and methods. Only abstract methods in an abstract class require a derived class (assuming derivation is not an abstract class) implementation, and the class implementing the interface must implement all the methods specified in the interface. In this way, if we add a new method to an interface, all classes that implement the interface need to be modified accordingly, and for abstract classes, we are adding non-abstract methods to them without any changes to their derived classes. 4. Interfaces and callbacks (callback)
Callback (callback) is a common design pattern that allows us to indicate what action should be taken when an event occurs. For example, there is a timer class in the Java class Library that allows notifications to be made after a specified time, just like an alarm clock. When initializing the time class, you need to specify an interval and the action to be performed after the time is reached. Here, tell the timer to perform the action that is passed to it by an object, and then arrive at the specified time, the timer will invoke the method of this object. The timer requires that the object we pass to it needs to implement the ActionListener interface, which is defined as follows:
Public Interface ActionListener { publicvoid actionperformed (ActionEvent event);}
The object we pass to the time constructor must define the Actionperformed method, in which we can define the actions we need to perform after the scheduled interval arrives. Take a look at the following code:
1 Public classTimertest {2 Public Static voidMain (string[] args) {3ActionListener listener =NewTimeprinter ();4Timer T =NewTimer (10000, listener);5 T.start ();6System.exit (0);7 }8 }9 Ten classTimeprinterImplementsActionListener { One Public voidactionperformed (ActionEvent event) { ADate now =NewDate (); -System.out.println ("The Time is" +Now ); - } the}
In the above code, the Actionperformed method in Timerprinter is called every 10 seconds to obtain and output the current time. The Actionperformed method is the callback method, in which the callback pattern is usually a class (Timeprinter) that implements an interface (ActionListener). The object of another class (Timer) then invokes the corresponding callback method by holding the class object reference (listener) that implements the interface.
5. Reference Java Core Technology (Volume 1) (watercress)
An in-depth understanding of Java interfaces