provided by: 
Originally published at Internet.comJava Programming Notes # 1495 * Preface * General Background Information * Discussion and Sample Code * The Program Named Reload03 * The Program Named Reload04 * The Program Named Reload05 * Run the Program * Summary * Complete Program Listings -----------------------------------
Preface
This is the second part of a two-part lesson on dynamic class loading and reloading.
In Loading/Reloading of Classes and Dynamic Method Invocation, Part 1, I taught you how to write a program that modifies its fundamental behavior at runtime by dynamically modifying, compiling, loading, and reloading classes. I also taught you how to instantiate an object of the newly-loaded class, and how to use reflection to invoke the instance methods belonging to an object of the newly loaded class.
In this lesson, I will teach you how to accomplish the same objectives while avoiding the complexities of reflection.
Three approaches
There are at least three ways to invoke methods on objects instantiated from dynamically-loaded classes. * Reflection * Runtime polymorphism based on interface inheritance * Runtime polymorphism based on class inheritance
Using reflection
For the completely general case where you have no influence over the design of the classes that are to be dynamically loaded, you probably have no choice but to use reflection and to endure the complexities of reflection. I demonstrated that approach in the programs named Reload01 and Reload02 in Part 1 of this lesson.
Runtime polymorphism based on interface inheritance
In some cases, you may be able to influence the design of the classes that are to be dynamically loaded. For example, you may be able to stipulate that the classes implement a common interface known both to you and the author of the new classes. In that case, you may be able to use runtime polymorphism bases on interface inheritance and to avoid the complexities of reflection. I will demonstrate this approach in the programs named Reload03 and Reload04 in this lesson.
Runtime polymorphism based on class inheritance
In other cases, you may have total control over the design of the classes that are to be dynamically loaded. In that case you may be able to use runtime polymorphism based on class inheritance to avoid reflection and also to avoid the requirement to create and to maintain a common interface. I will demonstrate this approach in the program named Reload05 in this lesson.
A practical example
As I mentioned in Part 1, whenever possible, I like to demonstrate Java programming concepts by writing a small but useful program that incorporates those concepts to advantage. After thinking long and hard about a small bbut useful program that clearly demonstrates the benefits of dynamic class loading, I finally settled on a program that writes, compiles, loads and executes new Java code on the fly with no requirement to stop and restart the program to incorporate the new code.
A plotting program
The program allows a user to enter chunks (multiple statements) of Java code into a text field during runtime. When the user enters a new chunk of code, checks a checkbox, and clicks a button, the new code is compiled, loaded, and executed as the body of a new method. The results produced by evaluating the method are then plotted in a Cartesian coordinate system as shown in Figure 1.

Figure 1
The user can repeat this process for as long as she has new chunks of code to evaluate with no requirement to stop and restart the program along the way.
From very simple to very complex
The chunk of Java code can be as simple or as complex as may be needed to satisfy the user's needs. For example, the horizontal blue line in Figure 1 resulted from the evaluation of the following simple equation:
y = -100; The black parabolic curve in Figure 1 resulted from the evaluation of the following equation:
y = x*x/100; Finally, the sloping red line in Figure 1 resulted from evaluating the following chunk of Java code, which includes the definition and use of a local inner class along with the getTime instance method from the Date class and the static sqrt method from the Math class. final double z = 65;class LocalClass{public long p = new java.util.Date().getTime();public double method( double data){return sqrt(p/(data*z*1.0E09));}}LocalClass q = new LocalClass();y = q.method(18.6)*x;
(Line breaks were manually inserted into the example given above to force it to fit into this narrow publication format.)
Viewing tip
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com.
General Background Information
I provided quite a lot of background information on class loading and reloading in Part 1 of this lesson. I won't bore you by repeating that material here. However, I will need to review some of that material in order to explain how the programs that I will
Author: Richard G. Baldwin
Read article at Internet.com site