Abstract Classes and Initiating sub classes dynamically

I have created an abstract class and three subclasses.  I am now working on code to dynamically load the subclasses by passing a string name of the subclass object.  Is this possible?  I am trying to eliminate the need for switch statements.  I am somewhat new to Java and OOP.

Who is Participating?
shaveriConnect With a Mentor Commented:
          The class "ClassLoader" is an abstract class. A class loader is an object that is responsible for loading classes. Given the name of a class, it should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system. Applications implement subclasses of ClassLoader in order to extend the manner in which the  Java virtual machine dynamically loads classes. For example, an application could create a network class loader to download class files from a server. Sample code might look like:
        ClassLoader loader = new NetworkClassLoader(host, port);
        Object main = loader.loadClass("Main",true).newInstance();

The network class loader subclass must define the methods findClass and loadClassData to load a class from the network.
A sample implementation is:

          class NetworkClassLoader extends ClassLoader {
              String host;
              int port;

              public Class findClass(String name) {
                  byte[] b = loadClassData(name);
                  return defineClass(name, b, 0, b.length);

              private byte[] loadClassData(String name) {
                  // load the class data from the connection
                   . . .
Also refer to the methods of this class,in order to get the detail idea........thanks
.. am now working on code to dynamically load the subclasses by passing a string name of the subclass object ...

can you post some sample 'pseudo' code ?
ubsjmgAuthor Commented:
I have actually figured out a way to do this using the Class Objects forName and newInstance methods.  The only question New question...Is this a valid and safe way to initiate objects?  I am adding this dynamic code so that I can data drive a process...When I build a new class with new functionality I can simply add the class name to a database.  Is this a poor OOP implementation?
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

maybe this is your need?
Class t = Class.forName("classname");
   certainly it's a bad way(i think, ^-^). But if you have specific reason, it's ok. so tell more about the reason.
I have used a lot of time similiar technique - for example my main program needs some Data Manager class - its behaviour is described inside the DataManager interface but the class that implements this interface can be dinamically configured (in fact the main app gets its name from a simple configuration file and tries to instantiate new Object and cast it to the DataManager interface)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.