Home  >  Article  >  Java  >  What is Java interface

What is Java interface

(*-*)浩
(*-*)浩Original
2019-11-14 13:35:302971browse

Java interface is a series of method declarations and a collection of method characteristics. An interface only has method characteristics but no method implementation, so these methods can be implemented by different classes in different places, and these implementations Can have different behaviors (functions).

What is Java interface

Two meanings:

1. Java interface, which exists in the Java language Structure has specific syntax and structure; (Recommended learning: java course)

Second, the set of characteristics of the methods possessed by a class is a logical abstraction. The former is called "Java interface" and the latter is called "interface".

Function

In the Java language specification, the characteristics of a method only include the name of the method, the number and type of parameters, and do not include the return type of the method, parameters name and the exception thrown.

When the Java compiler checks the overloading of a method, it will determine whether the two methods are overloaded methods based on these conditions. But when the Java compiler checks the substitution of the method, it will further check whether the return type and exception thrown by the two methods (supertype and subtype) are the same.

The rules for interface implementation and class inheritance are different. For data security, a class has only one direct parent class during inheritance, that is, single inheritance. However, a class can implement multiple interfaces, and interfaces make up for the inability of classes. Disadvantages of multiple inheritance: The dual design of inheritance and interface not only maintains the data security of the class but also implements multiple inheritance in disguise.

The Java interface itself does not have any implementation, because the Java interface does not involve representation, but only describes public behavior, so the Java interface is more abstract than the Java abstract class. But interfaces are not classes, and you cannot use the new operator to instantiate an interface.

Such as x=new comparable(...);//This is an error. But the interface variable Comparable x can be declared; //This is allowed.

Methods of Java interfaces can only be abstract and public. Java interfaces cannot have constructors. Java interfaces can have public, static and final attributes. That is, the properties in the interface can be defined as public static final int value=5;

The interface separates the characteristics of the method and the implementation of the method. This separation is reflected in the fact that an interface often represents a role, which packages operations and properties related to that role, and the class that implements this interface is the actor who plays this role.

A role is played by different actors, and apart from playing a common role, different actors do not require anything else in common.

The above is the detailed content of What is Java interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use == in javaNext article:How to use == in java