I have been going through a very simple example to set up a Remote Method Invocation application and while going through the client side code I am not able to understand one code as shown below. Definitely, gaps in my knowledge because I though an interface cannot have objects unless you use Anonymous Inner Class. So in the code below how did we creat an object of Remote Interface. It seems some sort of type-casting to me if I had to guess.
import java.rmi.*;
public class HelloClient {
public static void main(String args[]) {
try {
if (args.length < 0) {
System.err.println("usage: java HelloClient string …\n");
System.exit(1);
}
HelloInterface hello = (HelloInterface)Naming.lookup("//localhost/Hello");
This last line is what I am not able to understand what exactly is happening here with (HelloInterface) part?
HelloInterface hello = (HelloInterface)Naming.lookup("//localhost/Hello");
Naming.lookup()
call inspects the RMI Registry running in the localhost for a binding under the name "Hello".I thought an interface cannot have objects unless you use Anonymous Inner Class.
I have no idea what you're talking about here. Any class can implement an interface. This is rather basic.
So in the code below how did we create an object of Remote Interface
We didn't. We got it as a return value from the Registry, where it was put by the server.
It seems some sort of type-casting to me if I had to guess.
No need to guess. That's exactly what it is. There is only one 'sort' of typecasting, and this is how you write it. This is also rather basic.