May 10, 2021 Java
2. The declaration of the interface
3. The implementation of the interface
4. Inheritance of the interface
Interface, which is an abstract type in the JAVA programming language, is a collection of abstract methods, and interfaces are usually declared as interface. A class inherits the abstract method of an interface by inheriting it.
Interfaces are not classes, they are written in a similar way to classes, but they belong to different concepts. T he class describes the properties and methods of the object. The interface contains the method that the class wants to implement.
Unless the class implementing the interface is an abstract class, the class defines all methods in the interface.
Interfaces cannot be instantiated, but can be implemented. A class that implements an interface must implement all the methods described within the interface, or it must be declared as an abstract class. In addition, in Java, interface types can be used to declare a variable that can be an empty pointer or be bound to an object implemented by this interface.
Interfaces are similar to classes:
The declaration syntax format of the interface is as follows:
[可见度] interface 接口名称 [extends 其他的类名] {
// 声明变量
// 抽象方法
}
The Interface keyword is used to declare an interface. The following is a simple example of an interface declaration.
/* 文件名 : NameOfInterface.java */
import java.lang.*;
//引入包
public interface NameOfInterface
{
//任何类型 final, static 字段
//抽象方法
}
The interface has the following characteristics:
/* 文件名 : Animal.java */
interface Animal {
public void eat();
public void travel();
}
When a class implements an interface, the class implements all the methods in the interface. Otherwise, the class must be declared abstract.
The class implements the interface using the images keyword. In class declarations, the Implements keyword is placed after the class declaration.
To implement the syntax of an interface, you can use this formula:
... implements 接口名称[, 其他接口, 其他接口..., ...] ...
/* 文件名 : 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 above examples compile and run as follows:
Mammal eats
Mammal travels
When overrideing the methods declared in the interface, you need to be aware of the following rules:
There are some rules to keep in mind when implementing interfaces:
One interface can inherit the other, and classes inherit in a similar way. The inheritance of the interface uses the extends keyword, and the child interface inherits the method of the parent interface.
The following Sports interfaces are 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 itself and inherits two methods from the Sports interface, so that the class that implements the Hockey interface needs to implement six methods.
Similarly, the class that implements the Football interface requires five methods, two of which come from the Sports interface.
In Java, multiple inheritances of classes are illegal, but interfaces allow multiple inheritances.
The extends keyword only needs to be used once in the multiple inheritance of the interface, followed by the inherited interface. Here's what it looks like:
public interface Hockey extends Sports, Event
The above fragments are legally defined sub-interfaces, which, unlike classes, allow multiple inheritances, while Sports and Event may define or inherit the same method
The most common inheritance interface is an interface that does not contain any methods.
An identity interface is an interface without any methods or properties. It simply indicates that its class belongs to a particular type and is used by other code to test what is allowed to be done.
Identity interface function: A simple image is to mark (stamp) an object so that it has some or some privilege.
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
{}
Interfaces that do not have any methods are called tag interfaces. T he tag interface is primarily used for two purposes:
Like the EventListener interface, which is a Java API extended by dozens of other interfaces, you can use a tag interface to establish the parent interface of a set of interfaces. F or example, when an interface inherits the EventListener interface, the Java virtual machine (JVM) knows that the interface is going to be used for a proxy scenario for an event.
This is the original purpose of the tag interface, and the class implementing the tag interface does not need to define any interface methods (because the tag interface has no method at all), but the class becomes an interface type through polymorphism.