Only constants and abstract methods can be defined in an interface, right?

王林
Release: 2020-07-13 09:32:53
Original
17175 people have browsed it

Only constants and abstract methods can be defined in the interface, yes. In the Java language, an interface is an abstract type, a collection of abstract methods, usually declared with the keyword interface. A class inherits the abstract methods of the interface by inheriting the interface.

Only constants and abstract methods can be defined in an interface, right?

#Only constants and abstract methods can be defined in the interface, yes.

(Recommended learning:java introductory program)

Related introduction:

Interface

Interface (Interface), programming in JAVA In the language, it is an abstract type and a collection of abstract methods. An interface is usually declared as interface. A class inherits the abstract methods of the interface by inheriting the interface.

Interfaces are not classes. The way to write interfaces is very similar to classes, but they belong to different concepts. Classes describe the properties and methods of an object. The interface contains the methods that the class implements.

Unless the class that implements the interface is an abstract class, the class must define all methods in the interface.

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

(Video tutorial recommendation:java video tutorial)

Interface characteristics

  • Every method in the interface is also Implicitly abstract, the methods in the interface will be implicitly designated as public abstract (it can only be public abstract, other modifiers will report an error).

  • Interfaces can contain variables, but variables in the interface will be implicitly designated as public static final variables (and can only be public, using private modification will report a compilation error).

  • The methods in the interface cannot be implemented in the interface. The methods in the interface can only be implemented by the class that implements the interface.

The difference between abstract classes and interfaces

1. Methods in abstract classes can have method bodies, which can realize the specific functions of the methods, but in the interface method does not work.

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. The interface cannot contain static code blocks and static methods (methods modified 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.

The above is the detailed content of Only constants and abstract methods can be defined in an interface, right?. 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
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!