How to extend a base member field?
<p>I have a class (Base) and a field of type InfoBase holding some information. A
specialization (Ext) of Base needs to hold additional information (InfoExt).
Therefore Ext assigns an InfoExt to Base.info. However I ran into problems when
Base replaces info, since it would assign <code>info = new InfoBase()</code> hence the
additional info of InfoExt is lost.</p>

<p>Therefore I created an <code>abstract void assign()</code> in Base (variant A). In this
case info needs to be casted to InfoExt everytime it is used in Ext.</p>

<p>In variant B I have thus additionally created <code>abstract InfoBase info()</code>.</p>

<pre><code>                             variant A                        variant B
+----------------+  +---------------------------+  +----------------------------+
| InfoBase       |  | Base                      |  | Base'                      |
|----------------|  |---------------------------|  |----------------------------|
| + name: String |  | + info: InfoBase          |  | + abstract InfoBase info() |
|                |  | + abstract void assign()  |  | + abstract void assign()   |
|                |  |                           |  |                            |
+----------------+  +---------------------------+  +----------------------------+
          ^                      ^                               ^
          |                      |                               |
          +                      +                               +
+----------------+  +---------------------------+  +----------------------------+
| InfoExt        |  | Ext                       |  | Ext'                       |
|----------------|  |---------------------------|  |----------------------------|
| + id: int      |  | + void assign() {         |  | + InfoExt info             |
|                |  |     info = new InfoExt(); |  | + InfoBase info() {        |
|                |  |   }                       |  |     return info;           |
+----------------+  +---------------------------+  |   }                        |
                                                   | + void assign() {          |
                                                   |     info = new InfoExt();  |
                                                   |   }                        |
                                                   +----------------------------+
</code></pre>

<p>.</p>

<pre><code> class InfoBase {
   public String name;
 }

 abstract class Base {
    abstract public void assign();
    abstract InfoBase info();
 }


 class InfoExt extends InfoBase {
   public int id;
 }

 class Ext extends Base {
    public InfoExt info;

    @Override InfoBase info() { return info; }

    @Override public void assign() { info = new InfoExt(); }
 }
</code></pre>

<p>Is this a common situation with a generic way how to deal with it? Are there
any drawbacks to variant A/B?</p>

<p>How can I provide an info field in Base, that subclasses can use to store extended information?</p>

<p>Thank you for your consideration</p>
