search
HomeJavaJavagetting StartedOnly constants and abstract methods can be defined in an interface, right?

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

Jul 13, 2020 am 09:32 AM
constantabstract methodinterface

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!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft