Article View: borland.public.jbuilder.java.language
Article #17758Re: Runtime Casting?
From: Steve Demuth
Date: Fri, 09 Oct 1998 00:00
Date: Fri, 09 Oct 1998 00:00
93 lines
2874 bytes
2874 bytes
Jari,
The answer is to use interfaces as follows:
Interface Callbackable {
void callBack();
}
// Note addition of implements clause to your Class A:
Class A extends Object implements Callbackable {
B b = new B();
void doIt(){
//start here and call a run()-method on
// the class B
b.run(this);
}
void callBack(){
// do something
// here
}
}
Class B {
void run(Callbackable caller){
}
}
Jari Nurminen wrote:
> Hey you Gurus there,
>
> To implement a callback I try to make a following
> cast-decision at runtime:
>
> Class A extends Object{
> B b = new B();
>
> void doIt(){
> //start here and call a run()-method on
> // the class B
> b.run(this);
> }
> void callBack(){
> // do something
> // here
> }
> }
>
> Class B {
>
> void run(Object caller){
> // cast Object to A
> ((A)caller).callBack();
> // the parameter in run-method is Object because the calling object could
> also
> // be any class class which implements the callBack() method (different
> // inheritance tree and/or package from class A), but if the
> // caller object is e.q. a class 'D' I should edit all the casting calls
> which might
> // be many.
> // caller.callBack() doesn't compile because Object doesn't implement
> callBack()
> // caller.getClass() returns runtime class of the object (type Class), but
> the
> // caller.getClass().callBack() has same problem as the previous: it won't
>
> // compile (because caller.getClass() doesn't return the instance of
> caller)
> }
> }
>
> Is there a zero-admin solution to this when the caller object changes like:
>
> - A method which returns the instance of a class e.q. caller.getInstance()
> (hard to believe, because what would be the return type of that
> function?)
> - Casting using getClass() method e.q.
> (caller.getClass()).caller.callBack()
> (like this it doesn't work though). Since you can find out what is the
> runtime class of an object you should be able to cast some other objects
> to it, shouldn't you?
>
> I don't want any solutions how to implement it another way, the example was
> just to make clear what I'm talking about and the question is
> "is there this kind of runtime casting in Java?"
>
> cheers,
> Jari
Technical Details
Message-ID:
<361E38C1.4E3FC464@xyzq.salamander.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01bdf2b6$e19eeae0$2dd96fc2@pc12.gordion.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,2d1f5a5d309b076a X-Google-Attributes: gidf74b3,public From: Steve Demuth <demuth@xyzq.salamander.com> Subject: Re: Runtime Casting? Date: 1998/10/09 Message-ID: <361E38C1.4E3FC464@xyzq.salamander.com> X-Deja-AN: 399396201 Content-Transfer-Encoding: 7bit References: <01bdf2b6$e19eeae0$2dd96fc2@pc12.gordion.com> Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language