title: Generic interface for multiple sub types of an abstract class and dynamic casting
body: <p>I have a scenario that can be simplified like the following:</p>
  <ul>
  <li>I have a abstract class called <strong>AbstractField</strong></li>
  <li>There can be many classes that extend from this class : <strong>CurrencyField</strong>, <strong>DecimalField</strong>, etc.</li>
  <li>Now I have a generic interface <strong>FieldDBStore</strong> to create or update the field details in database.</li>
  <li>I want to implement the above interface for each type of field (whenever it is stored differently).</li>
  </ul>
  <p>Here is some code sample:</p>
  <p>For AbstractField</p>
  <pre><code>/* AbstractField */
  public abstract class AbstractField {
      //private members
      // builder
  }
  </code></pre>
  <p>For CurrencyField</p>
  <pre><code>public class CurrencyField extends AbstractField {
      //private members
      // builder that is extended from parent class's builder
  }
  </code></pre>
  <p>For FieldDBStore</p>
  <pre><code>interface FieldDBStore&lt;T extends AbstractField&gt; {
      
      void createField(T field, String tableName, String columnName);
      
      void updateField(T field);
  }
  </code></pre>
  <p>CurrencyFieldDBStore</p>
  <pre><code>public class CurrencyFieldDBStore implements FieldDBStore&lt;CurrencyField&gt; {
      @Override
      public void createField(CurrencyField field, String tableName, String columnName) {
          // Some operations
          
      }
  
      @Override
      public void updateField(CurrencyField field) {
          // Some operations
          
      }
  }
  </code></pre>
  <p>Now the problem: Whenever the client tries to send a field to be created/updated, it won't be knowing the field type. So, it will be sending the field as super type (AbstractField). This is resulting in the following error that is visible in eclipse</p>
  <pre><code>public AbstractField createAbstractField(AbstractField field, Long userId) { 
      //FieldType is an enum that can return appropriate DBStore implementation as per the field type
      FieldType type = field.getFieldType();
      String tableName = &quot;SomeTableName&quot;;
      String columnName = &quot;SomeColumnName&quot;;
      type.getFieldDBStore().createField(field, tableName, columnName);
  </code></pre>
  <p>In the above code, last line throws this error</p>
  <blockquote>
  <p>The method createField(capture#2-of ? extends AbstractField, String, String) in the type FieldDBStore&lt;capture#2-of ? extends AbstractField&gt; is not applicable for the arguments (AbstractField, String, String)</p>
  </blockquote>
  <p>I tried implementing an abstract field db store and then extending it, but it is not solving the issue. Any suggestions will be highly appreciated</p>