Loading, please wait...

A to Z Full Forms and Acronyms

What is Interface in Java?

Jun 24, 2021 Java, Java Interface, 4512 Views
Brief of Interface

What is Interface in Java?

What is Interface?

The interface is a blueprint of a class but it is not a class. Like class, it has methods and variables but all are constant variable and abstract methods. Abstract means it has only a method signature without the body. The interface is used to achieve abstraction in java and multiple inheritance in java. Without using an interface, you can’t achieve multiple inheritance in java. It specifies some set of methods that class must implement.

Syntax:

To declare an interface, the interface keyword is used. The methods are declared with an empty body. All the variables are constant.

interface interface_name
{

          //declare variables

          // declare abstract methods

}

Why do we use Interface in Java?

  • It helps in achieving an abstraction.
  • It is used in implementing multiple inheritances.
  • It is also used to achieving loose coupling.

Example of Interface

interface First_interface
{
   public void method1();
}
class Demo implements First_interface
{
   public void method1()
   {
	System.out.println("Hello");
   }
   public static void main(String arg[])
   {
	Demo obj = new Demo();
	obj.method1();
   }
}

OUTPUT:

Hello

 

In the above example, an interface is created with a name First_interface that contains a method1(). method1() is not having a body. Now, implement First_interface by using an implements keyword in the Demo class.

Example of Multiple inheritance implementation

interface demo1 {  
void print();  
}  
interface demo2 {  
void show();  
}  
class example implements demo1,demo2 {  
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome");}  
  
public static void main(String args[]){  
example obj = new example();
obj.print();  
obj.show();  
 }  
}  

 

OUTPUT:

Hello

Welcome

 

The above example is showing how you can achieve multiple inheritances with the help of interfaces. Multiple inheritances can not be achieved by the class because it causes deadly diamond. Interface solves the problem of diamond ambiguity. This we create a different interface and then implement those interfaces in the class using implement keyword.

Interface Inheritance

interface demo1{  
void print();  
}  

interface demo2 extends demo{  
void show();  
}  

class example implements demo2{  

public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome");}  
public static void main(String args[]){  
example obj = new example();
obj.print();  
obj.show();  
}  
}  

OUTPUT

Hello

Welcome

 

Advantage of Interface:

  • With the help of an interface, it is easy to achieve security.
  • Multiple Inheritance is implemented.

 Points to Remember:

  • The interface can not be instantiated. It means we can not create an object of an interface.
  • It provides full abstraction by creating a method without the body.
  • implements keyword is used for implementation.
  • It can not be declared as public, private, or static.
  • All the methods of an interface are by default abstract and public.
  • You need to initialize the variable otherwise it will give a compile-time error.
  • A class can implement n number of interfaces.

 

A to Z Full Forms and Acronyms

Related Article