title: Most efficient way to instantiate large number of subclasses in Java?
body: <p>I am working on a project that requires the use of a large number of objects of classes extending a particular abstract class. Instantiating manually requires a call to each object's individual constructor, which is starting to pollute my client class.</p>
  
  <p>The raw code from the project is too lengthy to post on this site, so I have a simplified example below:</p>
  
  <p>Suppose there is an abstract class called Letter:</p>
  
  <pre><code>public abstract class Letter{
      String letter;
  }
  </code></pre>
  
  <p>And 26 individual letter classes that extend the Letter abstract class:</p>
  
  <pre><code>public class A extends Letter{
      this.letter = "A";
  }
  
  public class B extends Letter{
      this.letter = "B";
  }
  ... and so on
  </code></pre>
  
  <p>This is where I seek help - There is a client class called Alphabet:</p>
  
  <pre><code>public class Alphabet{
      public List&lt;Letter&gt; getLetters(){
          List&lt;Letter&gt; letters = new ArrayList&lt;&gt;();
          A a = new A();
          B b = new B();
          C c = new C();
          list.add(a);
          list.add(b);
          list.add(c);
          ...the list goes on
  
          return letters;
      }
  }
  </code></pre>
  
  <p>As you can see, it is not ideal to instantiate each letter manually. I am looking for a way to create a list of class objects dynamically at runtime. </p>
  
  <p>Perhaps something similar to this:</p>
  
  <pre><code>public class Alphabet{
      public List&lt;Letter&gt; getLetters(){
          List&lt;Letter&gt; letters = getAllClassesExtending(Letter.class);
          return letters;
          //list would then contain A,B,C,D,E,F,G, etc.
      }
  }
  </code></pre>
  
  <p><strong>I ultimately want to be able to create a new class and have it automatically added to a list of objects in the client class, without needing to modify the client class (e.g. I do not want to explicitly reference each letter class in the client class).</strong></p>