• Interface is similar to an abstract class in that its members are not implemented.
  • In interfaces, none of the methods are implemented. There is no code at all associated with an interface.
  • Once it is defined, any number of classes can implement an interface.
  • One class can implement any number of interfaces.
  • To implement an interface, a class must create the complete set of methods defined by the interface.
  • Each class is free to determine the details of its own implementation.
  • By providing the interface keyword, java allows you to fully utilize the “One interface multiple methods” aspect of polymorphism.
  • Interfaces are designed to support dynamic method resolution at run time. For a method to be called from one class to another, both classes need to be present at compile time so that java compiler can check to ensure that the method signatures are compatible.
  • Interfaces add most of the functionality that is required for many applications which would normally resort to using multiple inheritances in C++.

Defining an Interface

Access-specifier interface-name
{
    Return-type method-name(parameter-list);
    Type final_varname1=value;
}
  • Where, Access-specifier is either public or default.
  • When no access specified is used, then default will be used and interface is only available to other members of the same package.
  • When it is declared as public, the interface can be used by any other code.
  • The methods which are declared have no bodies they end with a semicolon after the parameter list.
  • They are abstract method and each class that implements an interface must implement all of the abstract methods.
  • Variables can be declared inside of interface declarations.
  • They are final and static, means they can not be changed. They must also be initialized with a constant value.
  • All methods and variables are implicitly public if the interface is declared as public.

Example:

Interface Callback
{
   void callback(intparam);
}

Implementing Interfaces

  • Once an interface has been defined, one or more classes can implement that interface.
  • To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface.
  • The general form of a class that includes the implements looks like this:
Access class classname [extends superclass] [implements interface, [,interface..]]
{
   // class body
}
  • The methods that implement an interface must be declared as public.
  • The type-signature of implementing method must match exactly the type signature specified in the interface.
  • A class that implement interfaces may also define additional their own members and methods.
Class Client implements Callback
{
    public void Callback(int p)
    {
        System.out.println(“callback called with “+p);
    }
    void nonIfaceMeth()
    {
        System.out.println(“classes that implements
      interfaces may also define other members, too.”);

    }
}
Accessing Implementation through Interface References
  • We can declare variable as object references that use an interface rather than a class type.
  • Any instance of any class that implements the declared interface can be referred to by such a variable.
  • When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to.
  • The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them.
  • The following example, calls the callback() method via an interface reference variable.
Class TestIface
{
    public static void main(String args[])
    {
       Callback c= new Client();
       c.Callback(42);
    }
}

Output:

callback called with 42
  • Variable c is declared to be of the interface type callback, it was assigned an instance of client.
  • Although c can be used to access the callback() method, it cannot access any other members of the client class.
  • An interface reference variable only has knowledge of the method declared by its interface declaration.
  • Thus, c could not be used to access nonIfaceMeth() since it is defined by client.