1. Interface: A "special class" that abstracts classes more thoroughly, and can only contain abstract methods in the interface. The interface does not care about the internal state data of the class, and defines the specifications that a batch of classes comply with. (It only stipulates that certain methods must be provided in this batch of classes, and providing these methods can meet the actual requirements).
In the JAVA programming language, it is an abstract type, a collection of abstract methods, and interfaces are usually declared as interface. A class inherits the abstract methods of the interface by inheriting the interface.
Interfaces are not classes, and the way they write interfaces is very similar to classes, but they belong to different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.
Unless the class that implements an interface is an abstract class, the class defines all methods in the interface.
The interface cannot be instantiated, but can be implemented. A class that implements an interface must implement all methods described in the interface, otherwise it must be declared as an abstract class. Additionally, in Java, interface types can be used to declare a variable, they can be a null pointer, or bound to an object implemented with this interface.
Definition: [Modifier] interface interface name extends parent interface 1, assignment interface 2,...//No extends parent interface 1... is to define the interface
{
//Zero to multiple constant definitions...
//Zero to multiple abstract instance method definition...
}
[Syntax Description]: 1. The modifier can be public or omitted. If the public access control character is omitted, it is package access permission.
2. All members in the interface are publicly modified because the interface is public and can be accessed by all classes. The default modification of the attribute in the interface is: publicstaticfinal. Regardless of whether the attribute is written or not, the default modification of the same method is: publicabstract, internal class: publicstatic. They will be added by default.
3. There cannot be constructors in members, and there cannot be initialization blocks.
4. A java source file can only define at most one interface, because it is publicly modified, and the stored name must be the same as the name of the publicly modified class, and the interface can be regarded as a special class.
2. Interface inheritance: An interface can have multiple direct parent interfaces, and an interface can only inherit interfaces, but not classes.
[Note]: a. The child interface inherits the parent interface and will obtain all the abstract methods, constant attributes, internal classes and other definitions defined in the parent interface.
3. Interface usage: Syntax: [Modifier] class class name extends parent class implements interface 1, interface 2...{class body}.
【Precautions】
a. Description: A class can inherit a parent class and multiple interfaces at the same time, but extends must be placed after implements.
b. The interface cannot create an instance, but it can declare variables, but it must refer to the object of its implementation class.
c. The main purpose is to be implemented by class.
d. Implementation method: use the implements keyword
e. After a class inherits an interface, it must fully implement all the abstract methods defined in these interfaces (that is, rewrite these class methods in the subclass). Otherwise, if it is not fully implemented, then this class can only be defined as an abstract class.
f. When overriding the interface method, the modifier can only be larger or equal than the parent class, so it must also be a public modifier.
g. All interface type variables can be directly assigned to Object type variables.
Post the sample code (quoted to Li Gang's crazy java):
//Define an interface Output interface Output { //The attribute can only be defined as constants int MAX_CACHE_LINE = 50;//Mainly define storage space//The abstract method defined in the interface can only be public void out(); void getData(String msg); } //Define an interface Product interface Product { int getProduceTime(int a); } //Let the printer class implements defined interface public class Printer implements Output,Product { private String [] printData = new String[MAX_CACHE_LINE]; private int dataNum =0; public void out()//Rewrite the method of Output of the parent interface { while(dataNum>0) { System.out.println("Print Print"+printData[0]); System.arraycopy(printData,1,printData,0,--dataNum); } } public void getData(String msg)//Rewrite the method of Output of the parent interface { if(dataNum>=MAX_CACHE_LINE) { System.out.println("Full"); } else { printData[dataNum++] = msg; } } public int getProduceTime(int a)//Rewrite the method of the parent interface Product { return a = a; } public static void main(String[] args) { //The parent interface can declare variables, but cannot create instances, polymorphism: the declaration of Output, but the created instance is Printer Output O= new Printer(); O.getData("xiuxiu"); O.out();// Directly call the output method//The parent interface can declare variables, but cannot create instances, polymorphism: the declaration of Output, but the created instance is Printer Product p = new Printer(); System.out.println(p.getProduceTime(82)); //All interface type reference variables can be directly assigned to the Object type variable Object obj = p; Object obj1 = O; } }4. Similarities and differences between abstract classes and interfaces:
same:
1. All are abstract methods.
2. No instance can be created.
3. Subclasses must implement all the abstract methods within them after inheriting them. If they are not fully implemented, the class can only be defined as an abstract class.
Different:
1. There can only be abstract methods in the interface, and ordinary methods in the abstract class.
2. The attributes in the interface class have the modifier publicstaticfinal by default, but the attributes in the abstract class can be ordinary.
3. The interface does not have an initialization block, but it is found in the abstract class.
4. An interface can have multiple parent interfaces, but an abstract class can only have one parent class.
Summarize
The above is all about the interface and usage examples in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out.