An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.


Interface Class

An interface is similar to a class in the following ways −

 1.    An interface can contain any number of methods.

 2.  An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.

  3.   The byte code of an interface appears in a .class file.

 4.   Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including −

   1.   You cannot instantiate an interface.

   2.   An interface does not contain any constructors.

   3.   All of the methods in an interface are abstract.

   4.   An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

   5.   An interface is not extended by a class; it is implemented by a class.

   6.   An interface can extend multiple interfaces.

Example - 

interface A{

void show();  //public abstract

}

class B implements A{

public void show(){

System.out.println("show");

}

public static void main(String []arr){

B obj=new B();

obj.show();

}

}

Output -

        show


Example -

interface A{

int i=5;  //public static final

void show();

}

class B implements A{

public void show(){

System.out.println(i);

}

public static void main(String []arr){

B obj=new B();

obj.show();

}

}

Output -

        5

Multiple Interfaces

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

Example -

interface A{

void show();

}

interface B{

void display();

}

class C implements A,B{

public void show(){

System.out.println("show");

}

public void display(){

System.out.println("display");

}

public static void main(String []arr){

C obj=new C();

obj.show();

obj.display();

}

}

Output -

        show

        display