title: Java - How to choose what subclass to construct
body: <p>I have a parent class:</p>
  
  <pre><code>public class Animal {
    public Animal(String name, AnimalTypeEnum type) {
    }
  }
  </code></pre>
  
  <p>I have <strong>lots</strong> (in the case that has caused me to make this over 30) children:</p>
  
  <pre><code>public class Dog extends Animal {
    public Dog(String name, AnimalTypeEnum type) {
      super(name,type);
    }
  }
  
  public class Cat extends Animal {
    public Cat(String name, AnimalTypeEnum type) {
      super(name,type);
    }
  }
  </code></pre>
  
  <p>The objects are constructed from many different places in the project (loaded on start up, created at various times in runtime. To "create" an object, I currently use this function (over 30 subclasses in the project, not just 2):</p>
  
  <pre><code>public static Animal create(String name, AnimalTypeEnum type) {
    switch(type) {
      case DOG:
        return new Dog(name, type);
      case CAT:
        return new Cat(name, type);
    }
  }
  </code></pre>
  
  <p>It is important to note that none of the subclasses have any difference in parameters. The reason I'm using subclasses is because they have one or two functions that are overriden.</p>
  
  <p>Say I now wanted to add a parameter for all subclasses, I'd need to:</p>
  
  <ul>
  <li>Add the parameter to the <code>Animal</code> constructor</li>
  <li>Add the parameter to the subclass constructors</li>
  <li>Add the parameter to the <code>create()</code> function</li>
  <li>(also change the parameter wherever the <code>create()</code> function is being called)</li>
  </ul>
  
  <p>Ideally, I'd like to eliminate changing the subclass parameters (none of the subclasses have different parameters) and the <code>create()</code> function from the above process. But if that isn't possible, I'd like to find a better alternative to the <code>create()</code> function. The best solution I've thought of (<strong>psuedocode</strong>) is:</p>
  
  <pre><code>public Animal(String name, AnimalTypeEnum type) {
    if (type == CAT) this.subclass(Cat);
    if (type == DOG) this.subclass(Dog);
  }
  </code></pre>
  
  <p>I think the above goes against the principle of class inheritance so I want to be clear that this isn't the solution I am trying to find, just how I imagine a solution <em>could</em> work.</p>
  
  <p>This is a rather trivial issue but I feel that bad programming practices are behind it and I would like to rectify these. My questions would be (incase I haven't properly explained my problem/reasons):</p>
  
  <ul>
  <li>Is inheritance even the ideal way to do this?</li>
  <li>Is it possible to eliminate the process of changing the constructor in subclasses in this situation?</li>
  <li>Is there anything I can do to remove the <code>create()</code> function, and assign the subclass when constructing <code>Animal</code>?</li>
  </ul>
  
  <p>Thank you.</p>