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

Format and use of Java Interface

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

Share

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

This article mainly introduces "the format and use of the Java interface". In the daily operation, I believe many people have doubts about the format and use of the Java interface. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about the format and use of the Java interface. Next, please follow the editor to study!

The format of the interface and the format of using the interface

Unlike the class that defines the class, the interface keyword is required for interface definition.

The interface is still defined in a .java file, although a compiled .class file is still produced when declared using the interface keyword. This allows us to think of an interface as a special class that contains only functional declarations.

Define the format:

Publicinterface interface name {

Abstract method 1

Abstract method 2

Abstract method 3

}

The use of the interface

The methods in the interface are all abstract methods, and there is no point in calling methods directly through the new interface, and Java does not allow this.

The relationship between the class and the interface is the implementation relationship, that is, the class implements the interface. The action of the implementation is similar to inheritance, except that the keyword is different, and the implementation uses implements.

When other classes (implementation classes) implement the interface, it is tantamount to declaring that I should have the functionality in this interface. The implementation class still needs to rewrite methods to implement specific functionality.

Format:

Class class implements interface {

Override methods in an interface

}

After the class implements the interface, the class inherits the abstract method in the interface, and the class needs to rewrite the abstract method to complete the concrete logic.

Characteristics of members in the interface

1. Variables can be defined in the interface, but they must be modified with fixed modifiers, so the variables in the publicstaticfinal interface are also called constants, and their values cannot be changed. We'll talk about the fnal keyword later.

2. Methods can be defined in the interface, and methods also have fixed modifiers, publicabstract.

3. An interface cannot create an object.

4. The subclass must override all abstract methods in the interface before the subclass can be instantiated. Otherwise the subclass is an abstract class.

1.4 the relationship between interfaces and classes

A: between classes: inheritance relationship. A class can only inherit one parent class directly, but supports multiple inheritance.

B: between classes and interfaces: only implementation relationships, and a class can implement multiple interfaces

C: between interfaces: only inheritance relationships, and an interface can inherit multiple interfaces

The idea of interface

After learning the code embodiment of the interface, let's learn the idea of the interface and illustrate it from the examples in life.

For example: we all know that there are many sockets on the computer, and these sockets can be plugged into the corresponding equipment, why can these devices be plugged into them? The main reason is that these devices comply with the rules for the use of this socket during production, otherwise they will not be able to be plugged into the interface and can not be used. Finding that the appearance of this socket allows us to use more equipment.

The emergence of the interface is convenient for later use and maintenance, one side is using the interface (such as a computer), and the other is implementing the interface (the device plugged into the socket). For example, notebooks use this rule (interface), and computer peripherals implement this rule (interface).

Interfaces are widely used in the collection system:

Collection interface

List interface

ArrayList implementation class

LinkedList implementation class

Set interface

Interface advantages

1. The relationship between class and interface, implementation relationship, and multi-implementation, a class can implement multiple interfaces, the relationship between classes and classes is inheritance, inheritance in java is a single inheritance, a class can only have one parent class, breaking the limitations of inheritance.

two。 Provide rules (USB interface)

3. Reduce the coupling of the program (modular development can be realized, rules can be defined, everyone implements their own module, and the efficiency of development is improved)

At this point, the study on the format and use of the Java interface is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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