How to call interface method in Java?
#In order to call interface methods from a java program, the program must instantiate the interface implementation program. The method can then be called using the implementation object.
Example
public interface InterfaceDemo{ default public void displayNameDefault(String name){ System.out.println("Your name is : " + name); } public void displayName(String name); public void displayNameAndDesignation(String name, String designation); }
The above interface defines three methods to display the name and optional job title. One method is the default method that contains the implementation logic. The remaining two methods do not contain implementation logic.
public class InterfaceDemoImpl implements InterfaceDemo{ public void displayName(String name) { System.out.println(name); } public void displayNameAndDesignation(String name, String designation) { System.out.println("Name:" + name + "\n"+ "Designation:" + designation); } }
The above java program declares that it will use the implements keyword to implement the interface. The program is now obliged to provide java code for these two non-default methods. Accordingly, an implementation of this method is provided.
public class CallInterfaceMethod { public static void main(String args[]){ InterfaceDemo demo = new InterfaceDemoImpl(); demo.displayName("Adithya"); demo.displayNameAndDesignation("Adithya", "Java Developer"); demo.displayNameDefault("Adithya"); } }
The above program instantiates the interface implementation. Next, each method defined in the interface is called.
Output
Adithya Name:Adithya Designation:Java Developer Your name is : Adithya
The above is the detailed content of How to call interface method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

