This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
I agree with you...
Posted by Kishori Sharan on September 18, 2000 at 1:31 PM
You cannot cast an object just by having a class name in a string. So you have to use the class as
B bb = ( B ) Factory.create ( "B" ) ;
Just to simplify coding you can create static methods like createB ( ), createC ( ) which will return a reference of object of class B and C respectively and from within those methods call your original create ( "Classname" ) method , typecast the returned object to appropriate type and return it. This way you don't have to type cast the objects everytime you call create() and you can use
B bb = Factory.createB ( ) ;
I agree with you that Reflection is missing the functionality you have mentioned in your query.