Hi,
A RMI Client needs to know about the methods that the RMI server supports. Thus, it needs to know about the interface that the server class implements.
When the client makes a Naming.lookup() to lookup the server on the remote machine, it gets a remote stub object that is a proxy/local representative of the server object. The Proxy and the Server objects share the same interface (about which the client knows already) and thus, the client does not have any problems invoking the methods.
Also,
Java uses late binding (i.e. dynamic binding). So, you can subsititute one class for the other if they share the common ancestor.
to make this clear. Consider the following example.
public interface Server implements Remote
{
public void something() throws R...Ex...
}
public class ServerImpl extends Unicast.... implements Server
{
// register the server with the registry.
}
Client
------
public class Client
{
public static void main(
String argv[])
{
Server server = (Server) Naming.lookup("//host/SERVER"); }
}
In the above call to Naming, say if I replace it by
ServerImpl server = (ServerImpl) Naming.lookup("//host/SERVER");
and assuming that we have the ServerImpl class on the client side. The code would compile just fine but you would get a run-time exception (mostly ClassCastException) because
Naming.lookup returns the Stub class that is different from the ServerImpl class.
Thus for this arrangement to work, we need to program to interfaces and use late binding.
Hope this helps.
Ashwin.