search
HomeJavaJavaBaseWhat is Java interface

What is Java interface

Nov 14, 2019 pm 01:35 PM
java

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor