Java provides an alternate approach known as interfaces to support the concept of multiple inheritance. Although a Java class cannot be a subclass of more than one super class, it can implement more than one interface, thereby enabling the user to create classes that build upon other classes without the problems created by multiple inheritance.

Defining an Interfaces

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;
                        }

                        Here interface is the keyword and Interface Name is any valid Java variable. Variables are declared as follows:

            Static final type variableName=value;

All variables are declared as constants. Method declaration will contain only a list of methods without any body statements.

            Return-type methodName1 (Parameter list);

 

Declaring an Interface

          The rules to be considered while declaring an interface are

            1. All interface methods are implicitly public and abstract. No need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract.
2. All variables defined in an interface must be public, static, and final—
in other words, interfaces can declare only constants, not instance variables.
            3. Interface methods must not be static.
            4. Because interface methods are abstract, they cannot be marked final,
strictfp, or native.
5. An interface can extend one or more other interfaces.
6. An interface cannot extend anything but another interface.
7. An interface cannot implement another interface or class.
8. An interface must be declared with the keyword interface.

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 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.