This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes ClassCastException Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "ClassCastException Problem" Watch "ClassCastException Problem" New topic
Author

ClassCastException Problem

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
I have a business interface for data access: For example

In RMI, I have the same interface, say

Then I try to connect client across the network when server running and I try and cast and use the data layer interface

I get following error:
"java.lang.ClassCastException: Proxy cannot be cast to suncertify.db.HotelReservationDB"
Any reason why I can't cast, even though interfaces are same apart from remote exception?
Johnny Barbosa
Greenhorn

Joined: Sep 29, 2009
Posts: 26
Hi Mark,

Look, HotelReservationDB Interface is totally different of HotelReservationDBRemote interface.
You cannot to cast like this: "(HotelReservationDB)hotelReservation". To do it, is the same you want to transform "orange" in "apple". You can't to do this. Think about it. What do you really want to do?


Cheers,
Johnny Barbosa
SCJA, SCJP, SCWCD, SCJD(Story | Relato), SCBCD (coming soon)
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Cheers, thanks. Sorry, if I give impression I'm clueless, I was hoping to have cleaner code when calling the direct or network client connection by having the 1 interface reference. Is the only approach to this referencing to use a separate reference for each of the direct client and network client connection? Thanks very much for kind replies.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Mark!

Cheers, thanks. Sorry, if I give impression I'm clueless


Champion, we are all here to discuss and help each other. So... no need to worry, partner! We are glad to help and discuss

Now, please take a look at this sample code provided by my good buddy Roel to see if it helps.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Roberto Perillo wrote:Now, please take a look at this sample code provided by my good buddy Roel to see if it helps.

That's what I have been dreaming about for many weeks and months: being referenced by the Great Roberto


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Hahahaha

That's my good friend Roel!
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Cheers, thanks for all your help. Just wondering 1 final point, in my set-up I have, package-wise say:
BusinessService and LocalBusinessServiceImpl both in the db package.
BusinessRemote and NetworkBusinessServiceImpl in the remote package.
How can I justify throwing the remoteException for the methods in the BusinessService interface while these classes are supposed to be accessed locally? I hope one understands that I'm asking.
Or would I be better packaging the application differently to support throwing the RemoteException from the methods?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Well, for everything related to the business/services layer, I'd create a package called business or services.

Now, about the exception, I see your point... but if a method is defined in an interface and has the RemoteException in its throws clause, it means that it can throw it. The implementation doesn't need to include it in its throws clause if it won't be thrown.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Mark,

It is also shown in the provided: the method in the LocalBusinessServiceImpl is not throwing a RemoteException.

Regarding your package structure. I defined 3 packages: service (contains my business interface + service-exceptions), direct (for the local business implementations) and rmi (contains all classes to do the RMI-stuff).

Kind regards,
Roel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ClassCastException Problem
 
Similar Threads
implementing interface methods within another interface method
RecordNotFoundException in unlock method in Data class
RMI method invocation return value
NX:About DBMain interface
B&S 2.2.2 - Network Layer Query