> But I'm not sure how to implement polymorphism into code in order to
> enable adding new classes without the need for re-compilation --> Say
[quoted text clipped - 8 lines]
> was written class C didn't even yet exist ) without changing the
> source code of a program?
Not really. Polymorphism is limited to the method dispatch; using any
constructor or static method (i.e., methods invoked non-virtually)
requires the bytecode to work properly.
That said, it can be done via Java's Reflection, albeit less prettily.
> can you show me an example of code so that we could ( using the
> classes I declared in the above code ) call method of class C without
> changing the program's source code ( can this program be as simple as
> possible since I'm new at programming )?
public void test(B object) {
object.number();
}
Calling test with an argument of type C will invoke the method.
> Also, is it possible to start a program, and then, while this program
> is already running, add a new class to library and somehow use this
> new class in an already running program ( without restarting the
> program )?
Look into java.lang.ClassLoader and friends (i.e., reflection again).