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.

ubsjmgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
.. 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 ?
0
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?
0
zhongbingCommented:
maybe this is your need?
Class t = Class.forName("classname");
 
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

zhongbingCommented:
   certainly it's a bad way(i think, ^-^). But if you have specific reason, it's ok. so tell more about the reason.
0
shaveriCommented:
          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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heyhey_Commented:
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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.