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 is the use and principle of Java interface?

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

Share

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

Most people do not understand the knowledge points of this article "what is the use and principle of Java interface?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the use and principle of Java interface" article.

What is an interface?

Official explanation:

The interface in Java is the declaration of a series of methods and a collection of method characteristics. An interface only has the characteristics of methods but 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).

Personal understanding and explanation:

In fact, the interface can be understood as a special class, which is made up of global constants and * * common abstract methods (which need to be overridden). Interface is a means to solve the problem that Java cannot use multiple inheritance. We can understand the interface as a 100% abstract class, that is, the methods in the interface must all be abstract methods. Anyway, there are all methods in the interface, but there is no method body. Other classes can rewrite the method body after implementing this interface.

Why use the interface?

Interfaces are used to describe an abstraction.

Java can make up for the limitation that a class cannot inherit more than one class by implementing interfaces.

The interface is 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.

How to write the interface?

Create the keyword for the interface: interface

Key word to implement the interface: implements

Public interface StudentTest {/ / StudentTest: the name of the interface is public void Study (); / / the method body is not provided in the interface. } / / this is a simple interface

Implement the interface:

Public class student implements StudentTest {@ Override public void Study () {System.out.println ("I inherited StudentTest and overridden the Study method in the interface");}} / / Test class public class TestMain {public static void main (String [] args) {student s = new student (); s.Study ();}}

/ / output: I inherited StudentTest and overridden the Study method in the interface

/ / Code explanation:

/ / 1. Use interface to create StudentTest interface with Study method in it.

/ / 2. Create a new student class and implement the StudentTest interface with implements

/ / 3. Override the Study () method

/ / 4. Test class test

Note: to implement an interface, you need to rewrite all the methods in this interface!

The above code and explanation complete the preliminary understanding of the interface. Let's have a further understanding of the interface:

Learn more about the interface:

In life, we all have flash drives. To read and write to flash drives on a computer, we need to use the USB interface. A computer has multiple USB interfaces, so we first define two USB interfaces, and there are two methods, namely, read and write:

/ / first USB interface public interface USB1 {public void read_1 (); / / read method public void write_1 (); / / write method} / / second USB interface public interface USB2 {public void read_2 (); / / method public void write_2 () of the second USB disk;}

Then we have a U disk, because the U disk uses the standard USB interface, so we can read and write both USB interfaces. The U disk class implements USB1 and USB2 interfaces and rewrites the method:

Package instance_test;public class YOUPAN implements USB1,USB2 {@ Override public void read_1 () {System.out.println ("this is the read interface with the first USB"); @ Override public void write_1 () {System.out.println ("this is the write interface with the first USB");} @ Override public void read_2 () {System.out.println ("this is the read interface with the second USB"); @ Override public void write_2 () {System.out.println ("this is the write interface with the second USB");};} / / means that the U disk can be operated using any one as long as it meets the USB interface standard

Then we started using two interfaces:

Test class:

Public class Test_Main {public static void main (String [] args) {YOUPAN y = new YOUPAN (); y.read_1 (); y.write_1 (); y.read_2 (); y.write_2 ();}}

Results:

/ *

This is using the first USB read interface

This is using the first USB write interface

This is the read interface using the second USB

This is the write interface of the second USB

, /

Then we now have a mobile phone with a data cable with USB and Type_C at both ends. If we want to charge the phone and update data with the data cable, we need to implement both USB and Type_C interfaces, so we create a new Type_C interface:

Public interface Type_C {/ / Type_C interface public void Charge (); / / charging public void Update (); / / updating data}

Then create a new mobile class with a root data line and implement the USB interface and Type_C interface:

Public class phone implements USB1,Type_C {/ / data line implements USB1,Type_C interface @ Override public void read_1 () {System.out.println ("Mobile phone uses the first USB interface and") / / data line uses USB interface at the same time Will give a prompt} @ Override public void write_1 () {System.out.println ("phone uses the interface of the first USB and")} @ Override public void Charge () {System.out.println ("use the Type_C interface to charge") / / when the data line uses the Type_C interface, it will give a prompt} @ Override public void Update () {System.out.println ("use the Type_C interface to update data");}}

Then test the mobile phone class:

Public class Test_Main {public static void main (String [] args) {phone p = new phone (); p.read_1 (); p.Charge (); System.out.println ("-"); p.read_1 (); p.Update ();}}

Results:

/ *

The mobile phone uses the first USB interface and

Use the Type_C interface to charge

-

The mobile phone uses the first USB interface and

Use the Type_C interface to update data

, /

Such a class implements the implementation of multiple interfaces, which precisely solves the limitation that classes in Java cannot inherit multiple parent classes!

Summary of interfaces:

The interface cannot be instantiated directly, because the methods in the interface are abstract, there is no method body, and need to be rewritten

A class can implement multiple interfaces

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

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

Interface is used to make up for the limitation that classes cannot implement multiple inheritance

Interfaces can also be used to decouple (for decoupling, please refer to this article: understanding of decoupling)

Interfaces can also inherit, and can inherit more (yes, interfaces can inherit more! )

Public interface USB3 extends USB1,USB2 {} / / this multi-inheritance syntax is OK.

As you can see here, we already know that the interface is also abstract, so what's the difference between an interface and an abstract class?

The difference between interfaces and abstract classes (this is also an interview question, compiled from the Internet):

Comparison of abstract classes and interfaces:

Abstract classes are used to capture the common features of subclasses. An interface is a collection of abstract methods.

From the design level, the abstract class is the abstraction of the class, is a kind of template design, the interface is the abstraction of behavior, is a kind of behavior specification.

The similarities between the two:

Neither interface nor abstract class can be instantiated

Are at the top of inheritance and are used by other implementations or inheritance

All contain abstract methods, and their subclasses must override these abstract methods

The difference between the two is:

Note: default methods and static methods are introduced into interfaces in Java8 to reduce differences between abstract classes and interfaces. Now we can provide the default implementation method for the interface and do not have to force the subclass to implement it. Interfaces and abstract classes have their own advantages and disadvantages, and this principle must be followed in the selection of interfaces and abstract classes:

The behavior model should always be defined through interfaces rather than abstract classes, so interfaces are usually preferred and abstract classes are used as little as possible

When you choose an abstract class, you usually need to define the behavior of the subclass and provide common functionality for the subclass.

The above is the content of this article on "what is the use and principle of the Java interface". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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