Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the java interface mean?

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the java interface refers to, the article is very detailed, has a certain reference value, interested friends must read it!

Interface concept

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

My explanation: an interface can be understood as a special class that consists entirely of global constants and common abstract methods. Interface is a means to solve the problem that Java cannot use multi-inheritance, but in practice, interface plays a more role in setting standards. Or we can directly understand the interface as a 100% abstract class, since the methods in the interface must all be abstract methods. (JDK1.8 used to be understood this way)

Characteristics of the interface

Just like a class, an interface can have methods and properties, but the methods declared in the interface are abstract by default. (that is, only the method identifier, not the method body).

The interface indicates what a class must and cannot do, which is equivalent to the blueprint of the class.

An interface is to describe an ability, for example, "athlete" can also be used as an interface, and any class that implements the "athlete" interface must have the ability to run (or implement move () method), so the function of the interface is to tell the class that if you want to implement the function that the interface represents, you must implement some methods, so I can admit that you do have some ability that the interface represents.

If a class implements all the methods required in an interface, but does not provide a method body but only a method identity, then the class must be an abstract class. It is important to remember that abstract methods can only exist in abstract classes or interfaces, but there can be non-abstract methods in abstract classes, that is, methods with method bodies. Interfaces are 100% abstract classes)

An example of an interface in the JAVA library is the Comparator interface, which represents the ability to "compare". As long as any class implements the Comparator interface, the class also has the ability to "compare", so it can be used for sorting operations.

Why use the interface?

Interfaces are used to describe an abstraction.

Because Java doesn't support multiple inheritance like C++, Java can make up for this limitation by implementing interfaces.

Interfaces are also used to decouple.

Interfaces are used to implement abstractions, and abstract classes are also used to implement abstractions, so why must interfaces be used? What's the difference between an interface and an abstract class? The reason is that the abstract class may contain variables that are not final, but the variables that exist in the interface must be final,public,static.

Syntax implementation of interface

In order to declare an interface, we use the keyword interface, and all methods in the interface must only declare 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 need to manage the specific implementation. The property in the interface defaults to Public Static Final. A class that implements this interface must implement all the abstract methods defined in this interface.

A simple interface looks like this: it has global variables and abstract methods.

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

Among them, the testClass class implements the in1 interface we just defined above. Since you want to implement the interface, that is, a capability that the interface represents, you must implement the methods specified by the interface. Only when you implement all the abstract methods specified by the interface to you, do you recognize that your class implements this interface and implements some of the functions represented by this interface. The figure above implements the display () method specified in the interface.

Write a test class to test the interface we just implemented, because the object t of the testclass class implements the display method specified by the interface, so you can call the display () method naturally.

Further understanding of the interface

We know that if a device needs to read or write something to a computer, and these devices are generally connected to the computer by USB, we find that as long as the device with USB function can be plugged into the computer, then we can think that USB is a function, this function can do a lot of things (to achieve a lot of ways), in fact, USB can be regarded as a standard An interface, as long as the implementation of the USB standard devices, I think you already have the function of USB. (because you have implemented the method specified in my USB standard), here is a specific example:

Declare the USB interface first: it specifies that if you want to implement the USB interface, you must implement the read () and write () methods implemented by the interface specification.

Interface USB {void read (); void write ();}

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

Class YouPan implements USB {

@ Override

Public void read () {

System.out.println ("U disk is reading data through USB function");}

@ Override

Public void write () {

System.out.println ("U disk is writing data through the USB function");}} this is the specific implementation of U disk. Class JianPan implements USB {

@ Override

Public void read () {

System.out.println ("the keyboard is reading data through the USB function");}

@ Override

Public void write () {

System.out.println ("the keyboard is writing data through the USB function");}} this is the specific implementation of the keyboard.

So, now both U disk and keyboard have USB function, that is to say, both U disk and keyboard can call the methods specified in the USB interface, and they are all implemented in different ways.

We are writing a test to look at the specific implementation:

Public class Main {

Public static void main (String [] args) {

/ / generate a USB disk object that implements the USB interface (standard)

YouPan youPan = new YouPan ()

/ / call the read () method of U disk to read the data

YouPan.read ()

/ / call the write () method of U disk to write data

YouPan.write ()

/ / generate a keyboard object that implements the USB interface (standard)

JianPan jianPan = new JianPan ()

/ / call the keyboard's read () method to read the data

JianPan.read ()

/ / call the write () method of the keyboard to write data

The result of jianPan.write ();}} is as follows:

Interested students can go to the online IDE platform to verify for themselves: click on the open link

Several key points about the interface

We cannot directly instantiate an interface, because the methods in the interface are abstract and have no method body, so how is it possible to produce a concrete instance? However, we can use a reference to the interface type to point to an object that implements the interface, and we can call the methods in the interface. Therefore, the last method call in the figure above can also be written as follows: (actually using the polymorphic feature of Java)

Public class Main {

Public static void main (String [] args) {

/ / generate a USB disk object that implements the USB interface (standard)

/ / but use an interface reference to point to the object

/ / USB interface class references can point to an object that implements the USB interface.

USB youPan = new YouPan ()

/ / call the read () method of U disk to read the data

YouPan.read ()

/ / call the write () method of U disk to write data

YouPan.write ()

/ / generate a keyboard object that implements the USB interface (standard)

/ / but use an interface reference to point to the object

/ / USB interface class references can point to an object that implements the USB interface.

USB jianPan = new JianPan ()

/ / call the keyboard's read () method to read the data

JianPan.read ()

/ / call the write () method of the keyboard to write data

JianPan.write ();}}

two。 A class can implement more than one interface.

3. An interface can inherit from another interface, or some other interface, and an interface can inherit and inherit more than one interface.

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. Interface is used to make up for the limitation that classes cannot implement multiple inheritance.

7. Interfaces can also be used to decouple.

Popular understanding of interface

When we talk about polymorphism, we use the way of "air conditioning"-"remote control" to understand polymorphism. In fact, the first of the above key points also talks about the realization of polymorphism. For example, we can take "energy saving" as a standard, or energy saving is an "interface". There is a method in this interface, called frequency conversion method, any air conditioner, if it can be called energy-saving air conditioning. Then we must realize the interface of "energy saving" and the interface of "energy saving", and we must realize the "frequency conversion" method specified in the interface of "energy saving". Only in this way can we really realize the interface of "energy saving" and realize the function of "energy saving".

When an air conditioner implements the "energy saving" interface, the air conditioner has the function of energy saving, so we can also point to the air conditioning object without the reference of the air conditioner class. We can directly use a "remote control" referenced by the "energy saving" interface type to point to the "air conditioner", although this "remote control" has only one button and only one "frequency conversion" method. However, the air conditioner pointed to by the "remote control" realizes the interface of "energy saving", and there is the realization of the "frequency conversion" method. We use this remote control, which has only one "frequency conversion" method, to order the air conditioner to call the "frequency conversion" method. It also works.

Identification usage of the interface

Although there are some abstract methods defined inside the interface, not all interfaces must have methods, such as the Seriallizable interface. The function of the Seriallizable interface is to enable objects to be "serialized", but there is nothing in the Seriallizable interface, that is, if a class needs to implement the serialization function, the class must implement the Seriallizable interface. But instead of implementing methods (because there are no methods in the interface), the Serilizable interface is just an "identity" interface that marks a class that has this "serialization" function. For specific implementation, please refer to my other article-JAVA's IO stream.

The thought embodiment of Interface in Life

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

Children's shoes who play photography know that the SLR is made up of cameras and lenses, and there are different models of cameras, some half-frame and some full-frame. The lens is the same, divided into long focus, short focus, and fixed focus and zoom. Each shot has its own specific scene. It is precisely because of the diversity of lenses that our photography is able to "specialize in the art industry." Think about it, how bad it would be if our SLR camera part and lens part were fixed together and the lens could not be changed.

Therefore, in order to be compatible with different lenses, each camera brand has issued a set of standards for lens bayonets, which, like the "interface" we mentioned earlier, are some kind of "constraint". Take Chestnut, our Canon camera, no matter which lens manufacturer you are, Tenglong or Shima, as long as you produce lenses according to the standards of our Canon bayonet, the lenses you produce can be well driven on top of my Canon camera.

Therefore, when we turn on "something" and are ready to buy lenses for our new cameras, it is not difficult to find that we need to select specific bayonet lenses according to our camera brand, so that the lenses can be often driven by our cameras.

Back to the Java above, in fact, the biggest advantage of the interface is "decoupling", the camera can match different lenses, there can be a variety of matches to play, become more flexible. The same is true in software systems. Interfaces can have many different "characteristic" implementation classes. We only need to declare the same interface, but we can refer to many "subclasses" derived from the "interface". Doesn't this greatly enhance the flexibility of components in our software system?

The above is all the contents of this article "what does the java interface refer to?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report