What is a Java Interface

interface specifies what a class must do, but not how it does it.

To implement an interface, a class must create the complete set of methods defined by the interface.

Defining an Interface

An interface is defined much like a class. This is the general form of an interface:


access interface name { 
   return-type method-name1(parameter-list); 
   return-type method-name2(parameter-list); 
   
   type final-varname1 = value; 
   type final-varname2 = value; 
   
   // ... 
   return-type method-nameN(parameter-list); 
   
   type final-varnameN = value; 
}
Variables can be declared inside of interface declarations.
They are implicitly final and static.
They must also be initialized with a constant value.
All methods and variables are implicitly public if the interface, itself, is declared as public.

Here is an example of an interface definition.


interface MyInterface{ 
   void callback(int param); 
}
Home 
  Java Book 
    Class  

Interface:
  1. What is a Java Interface
  2. Implementing Interfaces
  3. Accessing Implementations Through Interface References
  4. Partial interface Implementations
  5. Variables in Interfaces
  6. Interfaces Can Be Extended