• An Interface is nothing but a kind of class.
  • Like classes, interface also contains methods and fields but with a major difference.
  • The difference is whatever the interface containing data, that is public, static and final at the time of compilation by default and whatever the interface containing method, that is public and abstract by default.
  • So the interface in java is a mechanism to achieve fully abstraction.


 

Why Interface

 

 

  • To support the concept of functionality of multiple inheritance.
  • To achieve fully abstraction, because by default the method in interface is public and abstract.
  • The most important point is interface achieves loose coupling, which is the main advantage of interface.

The general form of an interface is

interface InterfaceName
{
fields declarations;
methods declarations;
}
The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
Example
Interface Demo
{
int x=10;
void show();
}
Here the keyword interface tells that Demo is an interface containing one final field x and one abstract method show().

NOTE:

Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.

What we declare
interface Demo
{
     int x=100;
     void print();
}

What the Compiler Understand

interface Demo
{
     public static final int x=100;
     public abstract void print();
}
Let’s see a demo program for better understanding:

Example;

InterfaceExample1.java
 


package java8s;
interface Demo
{
     void print();
}

 

class Example2 implements Demo
{
     public void print()
     {
          System.out.println("JAVA means Silan Technology");
     }
}
public class InterfaceExample1 {

     public static void main(String[] args) {
         
          Example2 obj=new Example2();
          obj.print();
         

     }

}

Output
Interface

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment