You are here

Interface in Java

Interface in Java: Java interfaces are like Java classes but they contain only static final constants and declaration of methods. Methods are not defined and classes which implements an interface must define the body of method(s) of interface(s). Final constants can't be modified once they are initialized; final, interface, extend and implements are Java keywords.

Declaration of interface:

  1. interface InterfaceName {
  2.   // constants declaration
  3.   // methods declaration
  4. }

Interface program in Java

In our program we create an interface named Info which contains a constant and a method declaration. We create a class which implements this interface by defining the method declared inside it.

  1. interface Info {
  2.   static final String language = "Java";  
  3.   public void display();
  4. }
  6. class Simple implements Info {
  7.   public static void main(String []args) {
  8.     Simple obj = new Simple();
  9.     obj.display();
  10.   }
  12.   // Defining method declared in interface
  14.   public void display() {
  15.     System.out.println(language + " is awesome");
  16.   }
  17. }

Download Interface program class file.

Output of program:
Interface java example program output