title: How to reduce code by using a superclass?
body: <p>I would like to refactor some code that currently consists of a superclass and two subclasses.</p>
  
  <p>These are my classes:</p>
  
  <pre><code>public class Animal {
      int a;
      int b;
      int c;
  }
  
  public class Dog extends Animal {
      int d;
      int e;
  }
  
  public class Cat extends Animal {
      int f; 
      int g;
  }
  </code></pre>
  
  <p>This is my current code:</p>
  
  <pre><code>ArrayList&lt;Animal&gt; listAnimal = new ArrayList&lt;&gt;();
  
  if (condition) {
      Dog dog = new Dog();
      dog.setA(..);
      dog.setB(..);
      dog.setC(..);
      dog.setD(..);
      dog.setE(..);   
      listAnimal.add(dog);
  
  } else {
      Cat cat = new Cat();
      cat.setA(..);
      cat.setB(..);
      cat.setC(..);
      cat.setF(..);
      cat.setG(..);
      listAnimal.add(cat);
  }
  </code></pre>
  
  <p>How can I refactor the code regarding the common attributes? </p>
  
  <p>I would like something like that:</p>
  
  <pre><code>Animal animal = new Animal();
  animal.setA(..);
  animal.setB(..);
  animal.setC(..);
  
  if (condition) {
      Dog anim = (Dog) animal; //I know it doesn't work
      anim.setD(..);
      anim.setE(..);  
  } else {
      Cat anim = (Cat) animal; //I know it doesn't work
      anim.setF(..);
      anim.setG(..);
  }
  
  listAnimal.add(anim);
  </code></pre>