[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Dynamic Object type casting
We have already developed the "method" which sometimes returns "derived21[]"
and
other times "derived23[]". The method, looks into a parameters
(configuration) file
and appropritely calls routine which either return "derived21" type or
"derived23" types.
Because of the parameter/configuration file setup we also know that when it
returns
"derived21" and when it returns "derived23" object types. However, I was
just curious to
know if there is a way for us to typecast and use these, during runtime ?
I know that we can use java.lang.* and java.lang.reflect.* api for dynamic
objects, but
was curious to know if anybody has used this and come across this scenario ?
If yes then how do I do this ?
i.e. during the first call "derived21Instances" = (derived21[]) method(
"derived2" );
during the second call "derived23Instances" = (derived23[]) method(
"derived2" );
Any help will greatly be appreciated.
Thanks,
Nizar.
-----Original Message-----
From: Lukas Bradley [mailto:lukas.bradley@sententia.ws]
Sent: Monday, August 20, 2001 3:49 PM
To: ajug-members@ajug.org
Subject: RE: Dynamic Object type casting
You can't .... unless you are more specific in your method.
If you pass: (derived2) two seperate method calls, how can you expect
different data those two times?
In other words, if I call:
Math.max(2,3)
how can I expect anything other than 3 back?
Now, if you want random instantiations, or some outside static counter, this
is possible.
public derived[] method(derived myDerived)
{
derived[] lReturn = null ;
int lType = (int) (Math.random() * 4) + 1 ;
// you didn't tell us about length either
int lLength = (int) (Math.random() * 10) + 1 ;
lReturn = Array.newInstance(Class.forName(myDerived.getClass().getName()),
lLength) ;
// plus, this is uninitialized too
return lReturn ;
}
Or something like that.... I didn't compile it....
Lukas
> -----Original Message-----
> From: Bhamani, Nizar [mailto:nizar.bhamani@proactcorp.com]
> Sent: Monday, August 20, 2001 3:22 PM
> To: 'ajug-members@ajug.org'
> Cc: Bhamani, Nizar
> Subject: RE: Dynamic Object type casting
>
>
> To add on to my earlier email ...
>
> What I want is to dynamically assign these objects and use their methods
>
> i.e. during the first call "derived21Instances" = (derived21[]) method(
> "derived2" );
>
>
> during the second call "derived23Instances" = (derived23[]) method (
> "derived2" );
>
>
> How do I achieve this ?
>
> Thanks again,
>
> Nizar.
>
>
>
> > -----Original Message-----
> > From: Bhamani, Nizar
> > Sent: Monday, August 20, 2001 3:08 PM
> > To: ajug-members@ajug.org
> > Subject: Dynamic Object type casting
> >
> > All,
> >
> > Here is my scenario :
> >
> > Class baseclass;
> >
> > Class derived1 extends baseclass;
> > Class derived2 extends baseclass;
> >
> > Class derived11 extends derived1;
> > Class derived12 extends derived1;
> >
> > Class derived21 extends derived2;
> > Class derived22 extends derived2;
> > Class derived23 extends derived2;
> > Class derived24 extends derived2;
> > ...
> >
> >
> > I have a method that returns appropriate derived class based on
> parameter
> > passed :
> > i.e. public baseclass[] method( "derived2" )
> >
> > where 'method' returns array of objects of type : either "derived21", or
> > "derived22",
> > or "derived23", or
> > "derived24" ...
> >
> > Is there a way for me to dynamically typecast this return values ?
> >
> > i.e. during the first call to method( "derived2" ) would return an array
> > of "derived21" objects
> > during the second call it could return array of "derived23" and so on...
> >
> > Thanks in advance.
> >
> > Nizar Bhamani
> > ProActTechnologies
> > Powering Human Resourcefulness(tm)
> > (770) 291 7180
> > <mailto:Nizar.Bhamani@proactcorp.com>
> >
> >
>
>