Java interface
Interface (English: Interface) is an abstract type in the JAVA programming language and is a collection of abstract methods. Interfaces are usually declared with interface. A class inherits the abstract methods of the interface by inheriting the interface.
Interfaces are not classes. The way to write interfaces is very similar to classes, but they belong to different concepts. Classes describe the properties and methods of an object. The interface contains the methods that the class implements.
Unless the class that implements the interface is an abstract class, the class must define all methods in the interface.
Interface cannot be instantiated, but it 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. In addition, in Java, interface types can be used to declare a variable, they can be a null pointer, or be bound to an object that implements this interface.
Similarities between interfaces and classes:
An interface can have multiple methods.
The interface file is saved in a file ending with .java, and the file name uses the interface name.
The bytecode file of the interface is saved in a file ending with .class.
The corresponding bytecode files for the interface must be in a directory structure that matches the package name.
Interfaces cannot be used to instantiate objects.
Interface has no constructor method.
All methods in the interface must be abstract methods.
Interfaces cannot contain member variables, except static and final variables.
The interface is not inherited by the class, but implemented by the class.
Interfaces support multiple inheritance.
Interface declaration
The interface declaration syntax format is as follows:
[可见度] interface 接口名称 [extends 其他的类名] { // 声明变量 // 抽象方法 }
The Interface keyword is used to declare an interface. Below is a simple example of an interface declaration.
/* 文件名 : NameOfInterface.java */ import java.lang.*; //引入包 public interface NameOfInterface { //任何类型 final, static 字段 //抽象方法 }
- ##
Interfaces are implicitly abstract. When declaring an interface, you do not need to use the
abstract keyword.
-
Each method in the interface is also implicitly abstract, and does not require the
abstract key when declaring it.
All methods in the interface are public.
Instance
/* 文件名 : Animal.java */ interface Animal { public void eat(); public void travel(); }
Interface implementation
When a class implements an interface, the class must implement all methods in the interface. Otherwise, the class must be declared abstract.
The class uses the implements keyword to implement the interface. In a class declaration, the Implements keyword is placed after the class declaration.
To implement the syntax of an interface, you can use this formula:
... implements 接口名称[, 其他接口, 其他接口..., ...] ...
Example
/* 文件名 : MammalInt.java */ public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int noOfLegs(){ return 0; } public static void main(String args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } }
The compilation and running results of the above example are as follows:
Mammal eats Mammal travels
Rewrite the interface When declaring methods, you need to pay attention to the following rules:
When a class implements an interface method, it cannot throw a mandatory exception. It can only throw the mandatory exception in the interface or in an abstract class that inherits the interface.
Classes should maintain consistent method names when overriding methods, and should maintain the same or compatible return value types.
If the class that implements the interface is an abstract class, there is no need to implement the methods of the interface.
When implementing the interface, you should also pay attention to some rules:
A class can implement multiple interfaces at the same time.
A class can only inherit from one class, but can implement multiple interfaces.
An interface can inherit another interface, which is similar to inheritance between classes.
Interface inheritance
An interface can inherit another interface, which is similar to the inheritance method between classes. Interface inheritance uses the extends keyword, and sub-interfaces inherit the methods of the parent interface.
The following Sports interface is inherited by the Hockey and Football interfaces:
// 文件名: Sports.java public interface Sports { public void setHomeTeam(String name); public void setVisitingTeam(String name); } // 文件名: Football.java public interface Football extends Sports { public void homeTeamScored(int points); public void visitingTeamScored(int points); public void endOfQuarter(int quarter); } // 文件名: Hockey.java public interface Hockey extends Sports { public void homeGoalScored(); public void visitingGoalScored(); public void endOfPeriod(int period); public void overtimePeriod(int ot); }
The Hockey interface declares four methods and inherits two methods from the Sports interface. , In this way, the class that implements the Hockey interface needs to implement six methods.
Similarly, a class that implements the Football interface needs to implement five methods, two of which come from the Sports interface.
Multiple inheritance of interfaces
In Java, multiple inheritance of classes is illegal, but interfaces allow multiple inheritance.
In multiple inheritance of interfaces, the extends keyword only needs to be used once, followed by the inherited interface. As shown below:
public interface Hockey extends Sports, Event
The above program fragment is a legally defined sub-interface. Unlike classes, interfaces allow multiple inheritance, and Sports and Event may define or inherit the same method
Mark interface
The most commonly used inherited interface is an interface that does not contain any methods.
The identification interface is an interface without any methods or properties. It only indicates that its class belongs to a specific type for other code to test and allow to do something.
The function of the identification interface: To put it simply and vividly, it is to mark (stamp) an object so that the object has certain or certain privileges.
For example: The java.util.EventListener interface inherited by the MouseListener interface in the java.awt.event package is defined as follows:
package java.util; public interface EventListener {}
An interface without any methods is called a marker interface. Marker interfaces are mainly used for the following two purposes:
Establish a common parent interface:
Just like the EventListener interface, it is composed of dozens of A Java API that extends other interfaces. You can use a tagged interface to create a parent interface for a group of interfaces. For example: When an interface inherits the EventListener interface, the Java Virtual Machine (JVM) knows that the interface will be used for an event proxy scheme.
Add a data type to a class:
This situation is the original purpose of the marker interface. The class that implements the marker interface does not need to define any Interface methods (because marked interfaces have no methods at all), but the class becomes an interface type through polymorphism.