articles

Home / DeveloperSection / Articles / Interface in Java

Interface in Java

Anupam Mishra4884 11-Dec-2015

Interface are used to achieving fully Abstraction in java. If you want to achieve multi-threading in java then interface is an only way for that. By default, interface fields are public, static and final and methods are public and abstract. You can only defined abstract method in interface not giving the body of method. You are not create instance of interface same as abstract class.

If you want to implements more than one interface that are possible by using extends keyword in java. if you are not adding keyword public abstract before methods and public static final before fields then i.e. also compiled successfully. after compilation compiler will be adding itself.


The syntax of the Interface is as follow:

interface MyInterface { 
public abstract void displayName ();
}

For Example,

interface MyInterface 
{
       void DisplayName();
}
interface MyInterface2 extends MyInterface // extend another interface
{
       void DisplayAdd();
}
class TestingInterface implements MyInterface2
{
public void DisplayName()
{
       System.out.println("Anupam Mishra");
}
public void DisplayAdd()
{
           System.out.println("Allahabad");
}
public static void main(String args[])
{
       TestingInterface obj = new TestingInterface(); //instantiate object of the class
       obj.DisplayName();
       obj.DisplayAdd();
 }
 }  

Output:

Anupam Mishra

Allahabad


 

 


Updated 27-Mar-2018

Leave Comment

Comments

Liked By