title: In Java, can I store a class type in variable, where the class is some subclass of an abstract class?
body: <p>This question arose because of this <a href="https://github.com/ferortega/cf4j/blob/master/src/main/java/es/upm/etsisi/cf4j/examples/recommender/MatrixFactorizationComparison.java" rel="nofollow noreferrer">cf4j example code</a>. In particular, note that it creates a new QualityMeasure object for each recommender instance that is evaluated, and that <strong>if we wanted to switch our evaluation method / QualityMeasure subclass used (currently <code>RMSE</code>), we would have to search-and-replace each QualityMeasure constructor</strong>.</p>
  <pre><code>    // Evaluate PMF Recommender
      plot.addSeries(&quot;PMF&quot;);
      for (int factors : NUM_FACTORS) {
        Recommender pmf = new PMF(datamodel, factors, NUM_ITERS, RANDOM_SEED);
        pmf.fit();
  
        QualityMeasure rmse = new RMSE(pmf);  // NEED TO EDIT THIS...
        double rmseScore = rmse.getScore();
        plot.setValue(&quot;PMF&quot;, factors, rmseScore);
      }
  
      // Evaluate BNMF Recommender
      plot.addSeries(&quot;BNMF&quot;);
      for (int factors : NUM_FACTORS) {
        Recommender bnmf = new BNMF(datamodel, factors, NUM_ITERS, 0.2, 10, RANDOM_SEED);
        bnmf.fit();
  
        QualityMeasure rmse = new RMSE(bnmf);  // AND THIS...
        double rmseScore = rmse.getScore();
        plot.setValue(&quot;BNMF&quot;, factors, rmseScore);
      }
  
      // Evaluate BiasedMF Recommender
      plot.addSeries(&quot;BiasedMF&quot;);
      for (int factors : NUM_FACTORS) {
        Recommender biasedmf = new BiasedMF(datamodel, factors, NUM_ITERS, RANDOM_SEED);
        biasedmf.fit();
  
        QualityMeasure rmse = new RMSE(biasedmf);  // AND ALSO THIS...
        double rmseScore = rmse.getScore();
        plot.setValue(&quot;BiasedMF&quot;, factors, rmseScore);
      }
  </code></pre>
  <p>Is it instead possible to define a (fixed) variable that specifies what kind of subclass I will be using throughout?</p>
  <pre><code>   private static final QualityMeasureSubclass QM = RMSE;  // Can easily switch to MAE, MSE, etc.
  
     QualityMeasure qm = new QM(pmf);  // The constructor called depends on the subclass chosen above.
     double qmScore = qm.getScore();
     plot.setValue(&quot;PMF&quot;, factors, qmScore);
  </code></pre>
  <p>I do see that I can just factor out the common code, so I can adjust it in just that one place, but I don't know if the above is possible.</p>
  <pre><code>    private static double getQualityMeasureValue(Recommender rec) {
          QualityMeasure qm = new RMSE(rec);  // Only place we need to edit if we want to switch.
          return qm.getScore();
      }
  
      // Evaluate PMF Recommender
      plot.addSeries(&quot;PMF&quot;);
      for (int factors : NUM_FACTORS) {
        Recommender pmf = new PMF(datamodel, factors, NUM_ITERS, RANDOM_SEED);
        pmf.fit();
        plot.setValue(&quot;PMF&quot;, factors, getQualityMeasureValue(pmf));
      }
  </code></pre>
  <p>Edit: Cleaned up the last code section (no need for plot to be passed in).</p>