Interfaces

Définition

An interface is a completely "abstract class" that is used to group related methods with empty bodies, and constant fields.

To declare an interface  interface keyword must be used on the place of class keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static and final by default.
To inherit an interface  implements keyword must be used in the class definition.
The interfaces can inherit other intefaces by using extends keyword.  The interface specifies what all derived classes must do without specifying  how.

A class that implement interface must implement all the methods declared in the interface. If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.

Java does not support multiple inheritance in case of class, but by using interface multiple inheritance can be achieved.

public interface Int {
         void fx(int n) ;                  //  public, abstract 
         void gx() ;                         //  public, abstract  
}

class A implements Int, Int1, Int2 {
    // the methods from Int, Int1, Int2 must be defined here
}

public interface Int1 { .....}

public interface Int2 { .....}
    ...

Int intr;         //OK! 

Int ar[] = new Int[10];     // OK!

intr  = new Int();    // error!

intr  = new A();     // OK

Int intrf = new A(...) ; // OK

Int1 intrf1 = new A(...) ; // OK

Int2 intrf2 = new A(...) ; // OK

Example:

interface Modif {
    void zoom();
}
interface Displayable extends Modif{
    void dspl();
}
---------------------------------------
public class Two implements Displayable{
    private double value;
    Two(double v){
        this.value =v;
    }
    public void dspl(){
        System.out.println("double value: "+value);
    }
    public void zoom(){
        value/=2;
    }
}

public class One implements Displayable{
    private int value;
    One(int v){
        this.value =v;
    }
    public void dspl(){
        System.out.println("int value: "+value);
    }
    public void zoom(){
        value*=2;
    }
}
-------------------------------------------
public class Test {
    public static void main(String arg[]){
        Displayable tb[] = new Displayable[6];
        for(int i =0;i<tb.length;i++){
            double d;
            if((d=Math.random())>0.5){
                tb[i]=new One((int)(d*10));
            }
            else {
                tb[i]=new Two(d*10);
            }
        }
        for(int i=0;i<tb.length;i++){
            tb[i].dspl();
            tb[i].zoom();
            tb[i].dspl();
            System.out.println();
        }
    }

}
--------------------------------------------------
Result:
double value: 2.3030328288144633
double value: 1.1515164144072316

int value: 8
int value: 16

double value: 1.2622886977757242
double value: 0.6311443488878621

int value: 9
int value: 18

int value: 6
int value: 12

int value: 9
int value: 18