Home > Java > JavaBase > body text

Is java interface a class?

(*-*)浩
Release: 2019-11-15 11:08:17
Original
2805 people have browsed it

Is java interface a class?

#Java interface is not a class, because classes are defined using the class keyword and interfaces are defined using interface.                                                                                                                                                                                                                                                         # A collection of method characteristics.

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 used differently in different places. Class implementations, and these implementations can have different behaviors (functionality).
Two meanings:

One, Java interface, a structure that exists in the Java language, has specific syntax and structure;

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

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 the interface is not a class, 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 the 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.

Java interfaces (and abstract classes) are generally used as the starting point for a type hierarchy.

If a class already has a primary supertype, then by implementing an interface, the class can have another secondary supertype. This secondary supertype is called a mixed type.

The above is the detailed content of Is java interface a class?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!