Website For Developers

Interface in Java



Syntax

interface interfaceName {
  ........
  ........
}


Example of Interface in Java

interface Show {
   void output(); //public and abstract by default
}

class X implements Show {
   public void output(){ //public keyword is needed
     System.out.println( "Java Interface!" );
  }
}

class InterfaceExample {
   public static void main(String[] args) {
     X obj = new X();
     obj.output();
  }
}

Output

Java Interface!


Example of Multiple Inheritance in Java

interface Run {
  void running();
}

interface Walk {
  void walking();
}

//Implementing two Interfaces class MultiInherit implements Run,Walk {
  public void running() {
    System.out.println( "Car is Running ..." );
  }

  public void walking() {
    System.out.println( "Boy is Walking ... ");
  }

  public static void main(String args[]) {
    MultiInherit obj = new MultiInherit ();
    obj.running();
    obj.walking();
  }
}

Output

Car is Running ...
Boy is Walking ...