What is an interface in Java

Java provides the concept of interfaces that are very similar to the java classes. The difference is that java interfaces can hold multiple abstract methods. So, all in all, we can say that the Java interfaces are the blueprint of Java classes. The Java interfaces consist of static, abstract, and constant methods. An interface can hold only abstract methods(methods without definition) and constant fields.

In this write-up, we will learn the below-listed concepts of Java interfaces:

  • Interface in Java
  • Syntax
  • implements Keyword in Java
  • extends Keyword in Java
  • What is the Need For an Interface in Java
  • Examples

So, let’s get started!

Interface in Java

It is a blueprint/template of a java class that contains only abstract methods and constant attributes. This means an interface includes only the declaration of methods, and these methods will be defined in the implemented class. In java, interfaces can’t be instantiated.


The below code block will show the basic syntax of a java interface:

interface Example{
// constant fields/variable;
// abstract methods;

Here, “interface” is a keyword used to create an interface, while “Example” is a user-defined interface name.

Implements Keyword in Java

It is used to implement a java interface. Let’s have a look at the below snippet for a profound understanding of this concept:

class ExampleClass implements ExampleInterface{
//methods definition;

In the above snippet, a class named “ExampleClass” will inherit/implement the “ExampleInterface” using the “implements” keyword.

Similarly, a class can implement multiple interfaces using the below-given syntax:

class ExampleClass implements FirstInterface, SecondInterface{
//methods definition;

extends Keyword in Java

One interface can extend another interface using the “extends” keyword. Consider the below code block to understand the basic concept of the “extends” keyword:

interface FirstInterface{

interface SecondInterface extends FirstInterface {
  // code

Similarly, using the extends keyword an interface can extend multiple interfaces.

What is the purpose of using an Interface in Java

The below-given characteristics show the purpose of Java interfaces:

  • In java, interfaces are used to achieve multiple inheritance.
  • Interfaces in java provide Abstraction.
  • Loose coupling(class independence) can be achieved using Java interfaces.
  • In java, interfaces are public so that any other class can implement the interfaces.


In the below snippet, we created an interface named “Employee” and a class named “DemoClass”. The “DemoClass” class will implement the “Employee” interface:

interface Employee {
    void getData(int empId, String empName);
class DemoClass implements Employee {
    public void getData(int empId, String empName) {
        System.out.println("Employee ID: " + empId);
        System.out.println("Employee Name: " + empName);
 public static void main(String[] args) {
    DemoClass object = new DemoClass();
    object.getData(12, "Joe");

The Employee interface has an abstract method getData(int empId, String empName). The DemoClass implemented that method and defined it:

The output shows that the DemoClass successfully implemented the Employee interface.


This example will explain how an interface can extend another interface:

interface FirstInterface {
    void showData();
interface SecondInterface extends FirstInterface{
    void printData();

public class MainClass implements SecondInterface{
    public void showData(){
    public void printData(){
    System.out.println("Welcome to Linuxhint");
    public static void main(String[] args) {
    MainClass object = new MainClass();

In this example, we performed the below-listed functionalities:

  • Initially, we created two interfaces: FirstInterface and SecondInterface.
  • Both interfaces have an abstract method.
  • SecondInterface extended the FirstInterface.
  • After that, we created a class named MainClass.
  • The MainClass implemented the SecondInterface.
  • The MainClass defined the abstract methods.
  • Afterward, we created an object of the MainClass and accessed the methods of both the interfaces using that object.
  • Note that the MainClass didn’t implement the FirstInterface. Still, it defined the abstract method of FirstInterface and accessed it using the object of MainClass.

The MainClass implemented the SecondInterface while the SecondInterface extended the FirstInterface. Using SecondInterface, the MainClass can also access the FirstInterface. In this way, interfaces provide the functionality of multiple inheritance in java.


An interface in java is a blueprint or template of a class that contains only abstract methods and constant attributes. In java, interfaces provide numerous features, e.g., multiple inheritance, Abstraction, loose coupling(class independence), etc. This post presents a comprehensive overview of java interfaces with the help of relevant examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.