Like a class, an interface can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, nobody).

  • Interfaces specify what a class must do and not how. It is the blueprint of the class.
  • An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move (). So it specifies a set of methods that the class has to implement.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then grace must be declared abstract. 


interface <interface_name> {
// declare constant fields
// declare methods that abstract
// by default.

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in interface are declared with empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface. To implement interface
use implements keyword.

Why do we use interface?

  • It is used to achieve total abstraction.
  • Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance.

// A simple interface
interface Player
final int id = 10;
int move();

Example of Interface 

Use of Interface to achieve multiple inheritances

Let us say we have two interfaces A & B and two classes C & D. Then we can achieve multiple inheritances using interfaces as follows:
interface A{ }
interface B{ }
class C{ }
class D extends C implements A,B { }

Example Program 

Another Example