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
.