search
HomeJavaJavagetting StartedHow to call java interface

How to call java interface

Nov 14, 2019 pm 04:26 PM
javainterfacetransfer

How to call java interface

A class that implements a certain interface can call methods in the interface. An interface can be understood as an ability. For example, every animal has the ability to bark, but each animal has a different cry. The ability to bark can be defined as an interface.

How to call java interface

1. Create projects and packages

Open Eclipse and select File→New→Project. In the Select a wizard window that pops up, select Java Project and click Next.

How to call java interface

Enter the project name and click Next (it is recommended to follow the naming rules)

How to call java interface

Right-click the new project, Select New→Package in order. After naming the package, click Next.

How to call java interface

2. Create an interface

Right-click the package and select New→Interface.

How to call java interface

After naming the interface ShoutAbility, click Finish. (Shouting capability interface)

How to call java interface

As shown below, define the interface shout() method (keep good habits and add comments), and press Ctrl S to save.

How to call java interface

3. Create an interface implementation class

Right-click the package, select New→Class, and name the class AoShout ( Howling).

How to call java interface

As shown below, AoShout implements the ShoutAbility interface. The keyword to implement the interface is implements, and overrides the shout method of the interface. Press Ctrl S to save.

How to call java interface

4. Call the interface method

Create a Bird class and define a constructor with parameters, using the interface type to define parameters .

When defining Bird's shout() method, you can use interface variables to call the interface method.

Press Ctrl S to save.

How to call java interface

Create a test class, create an interface object using the interface implementation class AoShout, and then use the created bridShout object to create a bird object.

When calling the bird method, the method called to the interface will be executed, and the method of the implementation class will be found through the interface.

How to call java interface

Notes

The methods of the interface are all abstract methods, and the methods must be implemented in the implementation class; a class can have multiple An interface (capability); the first letter of the class name must be capitalized.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of How to call 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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor