Dynamic substitution of toString() implementation of the base class
<p>I have a class A with a </p>

<pre>
public String toString() {
}
</pre>

<p>method.</p>

<p>It gets compiled.
Also, I have <i><b>several</b></i> implementations of that method as a method body.
<i>This implementation uses A's fields.</i></p>

<p>I want to be able to dynamically substitute the contents of the toString() method
with such or such an implementation. The point here is that I need A's fields in the implementation.</p>

<p>I must say, I cannot create subclasses of A. I <b> must </b> use this class only.</p>

<p>As I can guess, each implementation must be compiled before the first usage.
But how would one do that?</p>

<p><hr>
The simplest solution would probably be to use a flag variable and analyze it before invoking such or such a method or even put everything into toString and use switch.
But what if A doesn't know anything about its implementation itself? Can they be decoupled - the A class and its toString implementations?</p>
