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); 
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.