aspose file tools*
The moose likes Distributed Java and the fly likes Java RMI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Java RMI" Watch "Java RMI" New topic
Author

Java RMI

Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Please excuse me if i am asking the obvious or something which has been previously discussed. I searched a little for this.... but i didnot know what exactly to search. I will keep searching on google to find out if I get an answer.

I have been exposed to RMI for the first time after learning Languages. One thing felt a little striking.



Consider the implementation




Now the client








I realized that the 2nd optioin is the right option. However, if I use the first, it gives me an error at runtime(Invalid cast). Now as far as I know, since the abstract interface method sayHello is defined only in the RMIImplementation class. So effectively any class which is returned by Naming.lookup will be a subclass of RMIImplementation and thus of RMIInterface.
If this is the case, why is there a problem in the casting?

What exacatly is the type of the returned object if it is not RMIImplementation but still is a type of RMIInterface
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Please assume minor mistakes are taken care of before compiling. and the boilerplate code is correct
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It will be a class created automatically by the JVM for this purpose; it won't be related to "RMIImplementation" in any way. In fact, the client program doesn't need the RMIImplementation class file to be on its class path.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

First of all, I'll move this to our Distributed Java forum - the place for RMI on JavaRanch.

Gaurav Raje wrote:I realized that the 2nd optioin is the right option. However, if I use the first, it gives me an error at runtime(Invalid cast). Now as far as I know, since the abstract interface method sayHello is defined only in the RMIImplementation class. So effectively any class which is returned by Naming.lookup will be a subclass of RMIImplementation and thus of RMIInterface.
If this is the case, why is there a problem in the casting?

What exacatly is the type of the returned object if it is not RMIImplementation but still is a type of RMIInterface

It's RMIImplementation_Stub. The rmic compiler creates two helper classes for your RMIImplementation class: RMIImplementation_Skel and RMIImplementation_Stub. These two wrap around your RMIImplementation class to handle the communication. The Stub class also implements the interface, and that's why casting to the interface works and casting to the original class doesn't.
You can verify as such:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Thanks for the reply....
I did check the class of the returned object. But i always thought it was still a subclass of the implementation. Hence was confused...

Thanks for clearing my doubt.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java RMI