Java Interface  

An interface is a logical grouping of variables and methods. Interface is used to achieve abstractionand concept of multiple inheritance. It cannot be used to create objects so it cannot contain a constructor.

0 240 Technology | Java
Thu May 16 , 2019

An interface is basically a kind of a class. Like Classes, interfaces contains methods and variables but with a major difference. The difference is that interface defines only abstract methods i.e. only method signature but no body and final fields.

Syntax: Interface declaration

interface <interfacename>
	variable declaration;     
	method  declaration;

Important Points

1. In an interface none of the methods contains its defination i.e. the defination or we can say body is provided by the "implement" class.

2. Interface each method that is declared by default public and abstract .

3. In an interface member variables cannot be left blank/uninitialised and once initialised they becomes public, static and final.

4. Interface cannot be instantiated just like the abstract class.

5. If a class implements an interface then it becomes mandatory for that class to define all the methods that have been declared in interface. The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members.

Example :

 interface ICalculation
void CalcSqure(int n1);
void CalcSum(int n1,int n2);
int x=10;

class InterfaceDemo implements ICalculation{
public void CalcSqure(int n1)
int sq=n1*n1;
System.out.println("Square of n1 is : "+ sq);
public void CalcSum(int n1,int n2)

public static void main(String args[]){
InterfaceDemo iobj = new InterfaceDemo ();
System.out.println("Value of x is: "+ iobj.x);
System.out.println("Value of x through interface is: "+ ICalculation.x);

Output :

Square of n1 is : 16
Value of x is: 10
Value of x through interface is: 10

Subscribe & Get Regular Updates On Your E-mail