How to add a method to a class on the fly? (not reflection related)
<p>Is it possible to achieve something like this in Java? My idea was to add a method to a class using annonymous classes but this doesn't seem to work.</p>

<pre><code>public class Abc {
    private ErrorsTable expectedErrorsTable = new ErrorsTable() {
        public ErrorsTable addError(String error) {
            this.add(error); //ErrorsTable is just a disguised ArrayList
            return this;
        }
    };

    ...

    public void someMethod() {
        expectederrorsTable.adError("abc");
    }
}
</code></pre>

<p>It is by no means a mistery why this doesn't work. The <code>expectedErrorsTable</code> is of type <code>ErrorsTable</code> so the compiler has no way of knowing that actually there lies an extended implementation equipped with <code>addError(String)</code>.</p>

<p>What other simple ways of achieving the same effect could be used (other than creating a new file with a new class extending <code>ErrorsTable</code>)?</p>
