Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What is the java interface?


May 30, 2021 Article blog


Table of contents


Interface concept

Official explanation: Java interface is the declaration of a series of methods, is a collection of method characteristics, an interface only the characteristics of the method has no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behavior (functions).

My explanation: Interfaces can be understood as a special class, which is made up entirely of global constants and common abstract methods. I nterfaces are a means of solving Java's inability to use multiple inheritances, but the interface's more useful role in practice is to set standards. O r we can directly understand the interface as a 100% abstract class, both of which must be all abstract methods. (As previously understood by JDK1.8)

Features of the interface

Like a class, an interface can have methods and properties, but the methods declared in the interface are abstract by default. (i.e. only method identifiers, not method bodies).

  • Interfaces indicate what a class must and cannot do, which is equivalent to a blueprint for a class.
  • An interface is to describe an ability, such as "athlete" can also be an interface, and any class that implements the "athlete" interface must have the ability to run this action (or implement move() method), so the purpose of the interface is to tell the class that if you want to implement the functionality that I represent on this interface, you have to implement certain methods, and I can admit that you do have some ability represented by that interface.
  • If a class implements all the methods required in an interface, but does not provide a method body but only a method identity, the class must be an abstract class. ( It is important to remember that abstract methods can only exist in abstract classes or interfaces, but non-abstract methods, i.e. methods with method bodies, can exist in abstract classes.) Interface is 100% abstract class)
  • An example of an interface in a JAVA library is the Comparator interface, which represents the ability to "compare" and can be used for sorting operations if any class implements this Composator interface.

Why use an interface

  1. Interfaces are used to describe an abstraction.
  2. Because Java doesn't support multi-inheritance like C+ , Java can make up for this limitation by implementing interfaces.
  3. Interfaces are also used to decouple.
  4. Interfaces are used to implement abstractions, and abstract classes are used to implement abstractions, so why do you have to use interfaces? W hat is the difference between an interface and an abstract class? The reason is that the inside of an abstract class may contain non-final variables, but the variables present in the interface must be final, public, static.

The syntax implementation of the interface

In order to declare an interface, we use the keyword interface, in which all methods must declare only the method identity, not the specific method body, because the implementation of the specific method body is implemented by the class that inherits the interface, so the interface does not use a tube-specific implementation. The properties in the interface default to Public Static Final. A class that implements this interface must implement all the abstract methods defined in this interface.

A simple interface is like this: with global variables and abstract methods.

 What is the java interface?1

To implement this interface, we use the implements keyword to implement the interface:

 What is the java interface?2

Where the testClass class implements the interface we just defined above in1, since you want to implement the interface, that is, the implementation of the interface represents a capability, then you must implement the interface to give you the prescribed method, only the interface to give you the abstract methods are implemented, only to recognize that you this class implemented this interface, the implementation of this interface represents a certain function. The figure above implements the display() method specified in the interface.

 What is the java interface?3

Write a test class to test the interface we just implemented, because the object t of the test class implements the display method specified by the interface, so it is natural to call the display() method.
 What is the java interface?4

Interested students can go to this online IDE to try it for themselves: click to open the link

A better understanding of the interface

We know that if a device needs to read or write something to the computer, these devices are generally USB connected to the computer, we found that as long as the device with USB function can be plugged into the computer to use, then we can think of USB is a function, this function can do a lot of things (to achieve a lot of methods), in fact, USB can be seen as a standard, an interface, as long as the implementation of USB standard device i think you already have U SB is a feature. (because you implemented the methods specified in my USB standard), here are some specific examples:

Declare the USB interface first: it specifies the two methods that must implement the interface provision implementation, read () and write () in order to implement the USB interface.

interface USB { void read(); v oid write(); }

Then write a USB drive class and a keyboard class, both of which implement the USB interface. (The method in which it is implemented)

class YouPan implements USB {
@Override
public void read() {
System.out.println ("U disk is reading data via USB function"); }
@Override
public void write() {
System.out.println ("U disk is writing data via USB function"); }}
这是U盘的具体实现。
class JianPan implements USB {
@Override
public void read() {
System.out.println ("Keyboard is reading data via USB function"); }
@Override
public void write() {
System.out.println ("Keyboard is writing data via USB function"); }}

这是键盘的具体实现。

So now both the USB disk and the keyboard have USB capabilities, which means that both the USB drive and the keyboard can call the methods specified in the USB interface, and they do so in different ways.

We're writing a test to see how it's implemented:

public class Main {
public static void main(String[] args) {
Build a USB stick object that implements a USB interface (standard).
YouPan youPan = new YouPan();
Call the read () method of the USB stick to read the data
youPan.read();
The werite () method that calls the USB stick is called to write data
youPan.write();
Build a keyboard object that implements a USB interface (standard).
JianPan jianPan = new JianPan();
Call the keyboard's read () method to read the data
jianPan.read();
Call the keyboard's site () method to write data
jianPan.write(); } }

结果如下:

 What is the java interface?5

Interested students can go to the online IDE platform to verify their own: click to open the link

Several key points about interfaces

  1. We can't instantiate an interface directly, because the methods in the interface are abstract and have no method body, how can we produce concrete instances? H owever, we can use a reference to an interface type to point to an object that implements the interface, and we can call methods in that interface. Therefore, the last method call in the image above can also be written as follows: (in effect, using polymorphic features in Java)
public class Main {
public static void main(String[] args) {
Build a USB stick object that implements a USB interface (standard).
However, use an interface reference to point to an object
A USB interface class reference can point to an object that implements a USB interface
USB youPan = new YouPan();
Call the read () method of the USB stick to read the data
youPan.read();
The werite () method that calls the USB stick is called to write data
youPan.write();
Build a keyboard object that implements a USB interface (standard).
However, use an interface reference to point to an object
A USB interface class reference can point to an object that implements a USB interface
USB jianPan = new JianPan();
Call the keyboard's read () method to read the data
jianPan.read();
Call the keyboard's site () method to write data
jianPan.write(); } }

2. A class can implement more than one interface.

3. One interface can be inherited from another interface, or some other interface, the interface can also be inherited, and can be inherited more.

4. If a class is to implement an interface, it must implement all the methods in that interface.

5. All methods in the interface are abstract and public, and all properties are public, static, final.

6. Interfaces are used to compensate for the limitations of classes that cannot achieve multiple inheritances.

7. Interfaces can also be used to decouple.

A popular understanding of interfaces

Earlier we talk about polymorphism with "air conditioning" - "remote control" way to understand polymorphism, in fact, in the above several key points of the first is also a polymorphic implementation, for example, we can "energy saving" as a standard, or energy saving is an "interface", this interface has a method, called variable frequency method, any air conditioning, if you want to be called energy-saving air conditioning, then we must achieve "energy-saving" this interface, "energy-saving" interface, then must also achieve "energy-saving" interface The interface provides for the implementation of the "variable frequency" method, so that it is a real realization of the "energy saving" this interface, the realization of "energy saving" this function.

When an air conditioner to achieve the "energy saving" interface, this air conditioner has energy-saving functions, then we can also use the air conditioning class reference to the air conditioning object, we can directly use a "energy saving" interface type reference "remote control" to point to "air conditioning", although this "Remote control "only one button above, only a "variable frequency" method, but "remote control" points to the air conditioning is to achieve "energy saving" this interface, there is a "variable frequency" method of implementation, we use this only one" The remote control of the variable frequency method to command the air conditioner to call the "variable frequency" method is also feasible.

The identity usage of the interface

Although some abstract methods are defined inside the interface, not all interfaces must have methods, such as the Seriallizable interface, which is used to "serialize" objects, but there is nothing in the Seriallizable interface, that is, if there is a class that needs to implement the "serialization" function, the class must implement the Seriallizable interface. H owever, the implementation method is not used (because there is no method in the interface), at this point, the Serilizable interface is simply an "identity" interface, which is used to flag a class that has this "serialization" function. For specific implementations, please refer to my other article, THE JAVA IO Stream.

Interfaces are embodied in the mind of life

In fact, in our life, there are many places reflect the "interface" of the idea, presumably, is reading this blog post you, do not also like photography?

 What is the java interface?6

Children's shoes that play photography know that SLR is composed of cameras and lenses, cameras are divided into different models, there are half-framed, but also full-frame. T he lens is the same, with telephoto, short focus, and fixed and zoom. E ach shot has its own specific play scene. I t is precisely because of the diversity of lenses that our photography can be "specialized in the art industry". Think about how bad it would be if our SLR camera part and lens part were fixed together and we couldn't change the lens!

Therefore, each camera brand in order to be compatible with different lenses, each issued a set of lens mount standards, this set of standards like the "interface" mentioned earlier, are some kind of "constraints." Take a chestnut, our Canon camera, no matter which lens manufacturer you are, Tenglong is good, the horse is good, as long as you follow the standards of my Canon card to produce lenses, you produce lenses can be very good on top of my Canon camera drive.

 What is the java interface?7

So when we turn on "Some East" and get ready to buy a lens for our new camera, it's not hard to see that we need to pick a specific camera based on our camera's brand, so that the lens can be driven by our camera properly.

 What is the java interface?8

Back to Java, in fact, the interface to us the biggest benefit is "decoupling", the camera can be paired with different lenses, in order to have a variety of matching play, become more flexible. In the software system is the same, the interface can have many different "features" of the implementation class, we only need to declare the same interface, but can refer to many of the "interface" derived from the "subclass", which does not greatly enhance the flexibility of the components in our software system?

Smart you, the understanding of the "interface" is not more in-depth?