Java interface

Java interface

Interface (English: Interface), in the JAVA programming language, is an abstract type and a collection of abstract methods. Interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.

An interface is not a class, the way of writing an interface is very similar to a class, but they belong to a different concept. A class describes the properties and methods of an object. An interface contains the methods to be implemented by the class.

Unless the class implementing the interface is an abstract class, the class defines all the methods in the interface.

An interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the 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 implemented by this interface.

Interfaces are similar to 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 interface is a bytecode file saved in a file ending with .class
  • The bytecode file corresponding to the interface must be in a directory structure that matches the package name

The difference between interface and class:

  • Interfaces cannot be used to instantiate objects
  • Structure has no constructor
  • All methods in an interface must be abstract
  • An interface cannot contain member variables, except static and final variables
  • Interfaces are not inherited, but implemented by classes
  • Interface supports multiple inheritance

Interface features:

  • Each method in the interface is also implicitly abstract, and the methods in the interface will be implicitly specified as public abstract (only public abstract, other modifiers will report errors)
  • The interface can contain variables, but the variables in the interface will be implicitly designated as public static final variables (and can only be public, and a compilation error will be reported when modified with private).
  • The methods in the interface cannot be implemented in the interface, only the class that implements the interface can implement the methods in the interface.

Difference between abstract class and interface

  • 1. A method in an abstract class can have a method body, that is, it can implement the specific function of the method, but the method in the interface cannot.
  • 2. Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type.
  • 3. An interface cannot contain static code blocks and static methods (methods decorated with static), while abstract classes can have static code blocks and static methods.
  • 4. A class can only inherit one abstract class, but a class can implement multiple interfaces.

Interface declaration syntax:

interface interface name [ extends other class name] {
         // declare variable
         // abstract method 
}

The interface has the following characteristics:

  • Interfaces are implicitly abstract. When declaring an interface, you do not have to use the abstract keyword.
  • Each method in the interface is also implicitly abstract, and the abstract keyword is also not required when declaring it.
  • The methods in the interface are all public (public)

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.

A class implements an interface using the implements keyword. In a class declaration, the Implements keyword is placed after the class declaration.

... implementsInterfaceName [, OtherInterfaceName, OtherInterfaceName..., ...] ...

When overriding a method declared in an interface, you need to pay attention to the following rules:

  • When a class implements a method of an interface, it cannot throw a mandatory exception. It can only throw the mandatory exception in the interface or in the abstract class that inherits the interface.
  • A class should keep the same method name when overriding a method, and should keep the same or compatible return type.
  • If the class implementing the interface is an abstract class, then there is no need to implement the methods of the interface.

When implementing an interface, 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.

Inheritance of interfaces

An interface can inherit from another interface, similar to the way classes inherit. Interface inheritance uses the extends keyword, and the subinterface inherits the methods of the parent interface.

The following Sports interface is inherited by the Hockey and Football interfaces:

// File name: Sports.java 
public  interface Sports
{
   public void setHomeTeam(String name);
   public void setVisitingTeam(String name);
}
 
// File 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 by itself, and inherits two methods from the Sports interface. In this way, a class that implements the Hockey interface needs to implement six methods.

Similarly, a class implementing 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 not legal, but interfaces allow multiple inheritance.

The extends keyword only needs to be used once in multiple inheritance of interfaces, followed by the inheriting interface. As follows:

public interface Hockey extends Sports, Event

The above program fragment is a legally defined sub-interface. Unlike classes, interfaces allow multiple inheritance, while Sports and Event may define or inherit the same method.

marker interface

The most common inherited interface is an interface that does not contain any methods.

A marker interface is an interface without any methods or properties. It simply indicates that its class belongs to a specific type, which other code can test to allow to do something.

The function of the marking interface: Simply put, it is to mark (a stamp) an object so that the object has one or some privileges.

For example, the java.util.EventListener interface inherited from the MouseListener interface in the java.awt.event package is defined as follows:

package java.util;
public interface EventListener
{}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445549&siteId=291194637