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]
# 1

> 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!

PaulRice at 2007-6-29 13:02:58 > top of Java-index,Archived Forums,Java Programming...
# 2

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

babbela at 2007-6-29 13:02:58 > top of Java-index,Archived Forums,Java Programming...