Abstract class
hello friends,
i have abstract class name=Abstract
i have implementing class name=implement
in a method i have got the name of the implementing class as implement.
when i do
Class c=Class.forName(implement)//it works fine.
Abstract a=new implement()//it works fine.
Abstract a=(Abstract)c//it doesn't work.
i have several implementing class.so at the run time i should be able to load the class.
i have to send the Abstract class as return
any clues.i know its very primitive.
thanks
sadac
[579 byte] By [
sadac] at [2007-9-26 4:04:28]

> hello friends,
> i have abstract class name=Abstract
> i have implementing class name=implement
>
> in a method i have got the name of the implementing
> class as implement.
> when i do
> Class c=Class.forName(implement)//it works fine.
> Abstract a=new implement()//it works fine.
>
> Abstract a=(Abstract)c//it doesn't work.
>
Why exactly are you using Class.forName? I'm not sure I understand what you are trying to do, but given an abstract class (we'll call it abst) and a class that implements abst (we'll call it impl) I think you should be doing something like this:
impl c = new impl();
abst a = new impl();
abst a = (abst) c;
I'm assuming right now you're getting a class cast exception? Class.forName return an object of type Class - not of the class you enter so you won't be able to cast it the way you want. Hope that helps!
Hi,
c is a class and not an instance of implement!
try
public Abstract getAbstract ()
{
try
{
Class c = Class.forName("Implement");
return (Abstract) c.newInstance ();
}
catch(Exception e)
{
}
return null;
}
bye