posted 23 years ago
You aren't sending an instance of an interface as a method parameter. You are sending a reference type whose class implements the methods in the given interface. So, the benefit is that the method can accept an instance of any class, as long as the class of the instance implements the given interface.
You can call the methods, or query the variables (static and final implicitly) of the interface. Suppose we have the following simple interface
public interface ABC
{
public static final int x = 0;
public void doSomething() throws Exception;
}
Then provide an implementation of that interface:
public class X implements ABC
{
public void doSomething() throws Exception
{
// implementation of doing something here.
}
}
you could then have a method defined (somewhere) such as
public void execute(ABC abc)
{
// Leverage the interface, without having to know anything about
// the class which implements ABC.
abc.doSomething();
}
which can be called by:
X x = new X();
object.execute(x);
where object is an instance of the class that provides the execute() implementation.