Implementing Interfaces
At once an interface has been defined; one or more classes could implement that interface. For implement an interface involve the implements clause in a class definition, and then create the methods defined through the interface. The common form of a class that involves the implements clause seems like this:
access class classname [extends superclass]
[Implements interface [interface ...] ] {
/ / class-body
}
In the above example, access is either public or not used. The interfaces are divided along with a comma if a class implements more than one interface. The similar method will be used through clients of either interface if a class implements two interfaces that declare the similar methods. The methods which implement an interface must be declared public. In addition, the type signature of the implementing method has to be match exactly the type signature specified in the interface definition.
The given below is small example class which implements the Callback interface displays earlier.
Class Client implements Callback {
/ / Implement Callback's interface
public void callback( int p ) {
system.out.println ("callback called with" + p)
}
}
Remember that callback ( ) is define using the public access specifier.
It is permissible and common both for classes which implement interfaces to declare further members of their own. For instance, the following version of Client implements callback ( ) and adds the method nonIface Meth ( ).
class Client implements Callback {
/ / Implement Callback's interface
public void callback (int p ) {
System.out.println ("callback called with" + p );
}
void nonifaceMeth ( ) {
System.out.println ("Classes that implement interfaces" +
"may also define other members, too." );
}
}