Java has basically three ways to accomplish this: classloaders, reflection and
the debugger API.
And one method that is relatively "nice", using a fairly simple subset
of reflection, is serialization.
Say you have a plugin architecture with, say, pluggable codecs. You
employ the strategy pattern and create a Codec interface that has
encode() and decode() methods, among others, and supply some
implementations. But you also provide code that runs at startup or
when a user picks "Scan for plugins" from a menu that looks in a /
plugins directory someplace looking for ".codec" files, which are
actually serialized instances of Codec objects. It reads these one by
one with an ObjectInputStream and casts each to Codec; if a
ClassCastException is thrown, it's ignored and so is the object that
was loaded; same if any of the various serialization exceptions or
IOExceptions are thrown. NoClassDefFound? Discard.
ObjectStreamException? Discard. The surviving objects are added to the
collection of Codec objects the program maintains. Perhaps there's an
initialize() method on these, and the ones that are newly added (using
a Set allows detecting which are really new) get this run, adding
particular "Save As" options that go through their respective encode()
methods. Perhaps the program tries to decode files opened with "Open"
by invoking each one's "decode()" in turn until one of them returns
something other than null (or doesn't throw some exception); the new
ones now get a crack at decoding any opened file.
This requires, on the programmer's part, no nastier reflection code
than code to deserialize objects (and, in some SDK somewhere, to
serialize them so plugins can be made). A plugin's installation
requires only that some .class files (perhaps wrapped in a .jar) go
into the application's classpath and a .codec file go in the
application's plugins directory. Likely that plugins directory is on
the app's class path and contains the plugin classes as well.
A .codec missing a needed .class will result in some sort of
deserialization exception; that particular one should probably trigger
a warning to the user, since it likely resulted from a sloppy
installation. A user adding a plugin and getting a silent failure of
anything new to appear in the program's menus will be much less able
to diagnose and fix the problem than one told that they forgot to
install one of the plugin's files (or the plugin's installer did).
It shouldn't be hard to generalize the above far beyond codecs to all
sorts of other plugin architectures.
The dynamic polymorphism here is of course in any of the instance
method calls through the Codec interface, and here can call code added
while the program is already running.