Implementing Interfaces

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 clause looks like this:


access-level class classname [extends superclass] [implements interface [,interface...]] { 
   // class-body 
}

Here is a small example class that implements the interface shown earlier.

 
interface MyInterface {
  void callback(int param);
}

class Client implements MyInterface{
  // Implement Callback's interface
  public void callback(int p) {

    System.out.println("callback called with " + p);
  }
}

callback() is declared using the public access specifier. When you implement an interface method, it must be declared as public.

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