
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to call an interface method in Java?
In order to call an interface method from a java program, the program must instantiate the interface implementation program. A 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 for displaying a name and optionally a job title. One method is a default method that contains implementation logic. The remaining two methods do not include 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 implement the interface by using the implements keyword. The program is now obligated to provide java code for the two non-default methods. Accordingly, implementations of the methods are 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 of the methods defined in the interface is called.
Output
Adithya Name:Adithya Designation:Java Developer Your name is : Adithya
- Related Articles
- Default method vs static method in an interface in Java?
- How to call a non-static method of an abstract class from a static method in java?
- How to declare, define and call a method in Java?
- Static method in Interface in Java
- Can we declare the method of an Interface final in java?
- How do we call a Java method recursively?
- How to access the fields of an interface in Java?
- How to write a class inside an interface in Java?
- How to implement an interface using an anonymous inner class in Java?
- How can we call the invokeLater() method in Java?\n
- Java Program to pass method call as arguments to another method
- Why an interface cannot implement another interface in Java?
- How to call an activity method from a fragment in Android App?
- What happens if we define a concrete method in an interface in java?
- How to write/declare an interface inside a class in Java?
