An interface is basically a kind of class. Like classes, interfaces contain methods and variables but with a major difference. The difference is that interfaces define only abstract methods and final fields. This means that interface do not specify any code to implement these methods and data fields contain only the constants. Therefore it is the responsibility of the class that implements an interface to define the code for implementation of these methods.

                        An interface is a contract for what a class can do, without saying anything about how the class will do it. Interfaces are like a 100-percent abstract super class that defines the methods a subclass must support, but not how they must be supported.

            The syntax for defining an interface is as follows:
                        interface InterfaceName
                        {
                                    variables declaration;
                                    methods declaration;
                        }

 

An example for an interface is:

   public interface Shape {
double PI = 3.14; // static and final => upper case
void draw(); // automatic public
void resize(); // automatic public
}

Extending interfaces
            Like classes, interfaces can also be extended. That is, an interface can be sub interfaced from other interfaces. The new sub interface will inherit all the member of the super interface in the manner similar to sub classes. This is achieved using the keyword extends as shown below:

            public class Square extends Rectangle {
public void draw() {System.out.println ("Square"); }
public void resize() { /* do stuff */ }
}
While interfaces are allowed to extend to other interfaces, sub interfaces cannot define the methods declared in the super interfaces. After all, sub interfaces are still interfaces, not classes. It is the responsibility of any class that implements the derived interface to define all the methods. When an interface extends two or more interfaces they are separated by commas. It is important to remember that an interface cannot extend classes.

Implementing interfaces

            Interfaces are used as “super classes” whose properties are inherited by classes. It is therefore necessary to create a class that inherits that inherits the given interface.
           
Example:
            //Interface. Java
            interface Area{
                        final static float pi=3.14F;
                        float compute (float x,float y);
            }

public class Rectangle implements Area {
public float compute (float x,float y)
{         
return(x*y);
}
}

class InterfaceTest {
            public static void main(String args []){
            Rectangle rect = new Rectangle();
            Area area;
            area=rect;
            System.out.println(“Area of Rectangle”+area.compute(10,20);

           
}

}

Any number of dissimilar classes can implement an interface. Note that if a class implements an interface does not implement all the methods of the interface, then the class becomes an abstract class and cannot be instantiated.