Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java interface


May 10, 2021 Java


Table of contents


Java 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:

  • An interface can have multiple methods.
  • The interface file is saved in .java end of the file, and the file name uses the interface name.
  • The bytecode file for the interface is saved in .class end of the file.
  • The bytecode file for the interface must be in the directory structure that matches the package name.
The difference between an interface and a class:
  • Interfaces cannot be used to instantiate objects.
  • The interface does not have a construction method.
  • All methods in the interface must be abstract.
  • Interfaces cannot contain member variables, except for static and find variables.
  • The interface is not inherited by the class, but is to be implemented by the class.
  • The interface supports multiple inheritances.

The declaration of the interface

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:
  • Interfaces are implicitly abstract and do not have to use the abstract keyword when declaring an interface.
  • Each method in the interface is also implicitly abstract, and the abstract key is not required to declare.
  • The methods in the interface are all public.

Instance

/* 文件名 : Animal.java */
interface Animal {

   public void eat();
   public void travel();
}

The implementation of the interface

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 接口名称[, 其他接口, 其他接口..., ...] ...

Instance

/* 文件名 : 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:

  • When a class implements an interface's method, it cannot throw a mandatory exception, only in the interface, or in an abstract class that inherits the interface.
  • Classes should have consistent method names when overrideing methods, and should maintain the same or compatible return value type.
  • If the class that implements the interface is an abstract class, there is no need to implement the method of the interface.

There are some rules to keep in mind when implementing interfaces:

  • A class can implement multiple interfaces at the same time.
  • A class can inherit only one class, but it can implement multiple interfaces.
  • One interface can inherit another interface, which is similar to inheritance between classes.

Inheritance of the interface

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.


Multiple inheritance of the 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


Tag the interface

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:

  • To establish a common parent interface:

    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.

  • Add a data type to a class:

    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.