title: How to instantiate subclasses in superclass?
body: <pre><code>public class Base{
      protected Base instantiate() {//Should return an instance of the subclass that this method has been called from
  
  
      }
  }
  
  public class Sub1 extends Base {
  
  }
  
  public class Sub2 extends Base {
  
  }
  
  public static void main(String[] a) {
      Sub1 sub1 = new Sub1();
      Sub2 sub2 = new Sub2();
      Sub1 ss = sub1.instantiate();//should return a new instance of Sub1 
      Sub2 s = sub2.instantiate();//should return a new instance of Sub2
  }
  </code></pre>
  
  <p>How can i  instantiate subclasses in superclass. In other words i need to know the implementation of the instantiate() method in Base class in above code.</p>
  
  <p><strong>Edit</strong>: This is part of the actual code:
  the base class is:</p>
  
  <pre><code>     /**
           * Any class that extends this class should override the TABLE_NAME
           *
           * @author sSh
           */
          public class Model {
  
              protected static final String TABLE_NAME = null;
  
              private void createRecord() throws ClassNotFoundException, SQLException {
  
  
              }
  
              private void updateRecord() {  
  
              }
  
              public ResultSet getAllRecords() { 
  
                  //This is the method--that returns instance of the subclass that this method has 
     // been called from
              }
  
              public void saveRecord() throws SQLException, ClassNotFoundException {
  
              }
  
  
          }
  </code></pre>
  
  <p>The Model class should be the Base class of all the models.
  so if we have PatientModel it would be something like this:</p>
  
  <pre><code>public class PatientModel extends Model{
  
          protected static final TABLE_NAME = "patient";
  
          //...fields and methods
  
  
      }
  
  
      public static void main(String[] a){
          PatientModel.getAllRecords();//this will return all the records in patient table as patient objects
  
      }
  </code></pre>