🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #17756

Runtime Casting?

#17756
From: Jari Nurminen
Date: Fri, 09 Oct 1998 00:00
59 lines
2149 bytes
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: <01bdf2b6$e19eeae0$2dd96fc2@pc12.gordion.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,2d1f5a5d309b076a,start
X-Google-Attributes: gidf74b3,public
From: "Jari Nurminen" <nospam_nurminen@gordion.com>
Subject: Runtime Casting?
Date: 1998/10/09
Message-ID: <01bdf2b6$e19eeae0$2dd96fc2@pc12.gordion.com>
X-Deja-AN: 399254183
Organization: Gordion- Business Consulting Ltd.
Newsgroups: borland.public.jbuilder.java.language