Re: java.lang.ClassCastException

Leo Mekenkamp <[email protected]>
Newsgroups gmane.comp.java.ozone.user
Message-ID <1078403826.28845.49.camel@mars>
On Thu, 2004-03-04 at 12:34, Sunil Goyal wrote:
> Hello all,
> 
> I am having a trouble when using protected methods for updating objects.
> and get a class cast exception.
> 
> interface A extends OzoneRemote{
> }
> 
> class AImpl extends OzoneObject implements A{
> protected void add(B b){
> // any implementation
> }
> 
> }
> 
> interface B extends OzoneRemote{
> public void somemethod(A a);
> }
> 
> class BImpl extends OzoneObject implements A{
> 
> public void somemethod(A a){
> 
> ((AImpl)a).add((B)self());
> 
> }
> }
> 
> When I call the method somemethod() in a test class, I get
> a class cast exception. The class cast exception, I think is because
> I am trying to cast the object into AImpl whereas it actually is
> instance of AImpl_Proxy .

Correct.

> But as the method is protected, I can't cast this
> into "A" .
> What is the solution for this?

There is none. java does not have any provisions for private or
protected methods in an interface. You can more or less work around this
by separating 'protected' methods into a different interface and have
your implementation implement both interfaces.

Cheers,
Leo




-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.