title: Passing an interface object in a constructor as an arguement
body: <p><strong>How do I pass an interface object, as an argument in a concrete class?</strong></p>
  
  <p>I'm creating an animal simulator where foxes eat rabbits. The foxes and rabbits have the ability to reproduce. And both animals also have the ability of dying of old age. So that's what i'm creating and that's the concept.</p>
  
  <p>I want to pass a Factory object as an argument in the Simulator class so that in the future, I can easily create more types of factories. For example, a VirusFactory where viruses can kill an Animal and multiply over time...etc etc. </p>
  
  <p>So the Interface Factory class looks like this:</p>
  
  <pre><code>public interface Factory
  {
      //currently empty
  }
  </code></pre>
  
  <p>The AnimalFactory concrete class creates the animals!. It implements the Factory interface.</p>
  
  <pre><code>public class AnimalFactory implements Factory
  {
       //Code omitted
  }
  </code></pre>
  
  <p>I have a simulator class. In the simulator class, I want to pass a Factory object as an argument to the Simulator constructor. How can this be done?</p>
  
  <pre><code>public class Simulator
  {
      // Constants representing configuration information for the simulation.
      // The default width for the grid.
      private static final int DEFAULT_WIDTH = 100;
      // The default depth of the grid.
      private static final int DEFAULT_DEPTH = 100;   
  
      // List of actors in the field.
      private final List&lt;Actor&gt; actors;
  
      // The current state of the field.
      private final Field field;
  
      // The current step of the simulation.
      private int step;
  
      // A graphical view of the simulation.
      private final SimulatorView view;
  
      // A factory for creating actors - unused as yet.
      private final AnimalFactory factory;
  
          /**
       * Construct a simulation field with default size.
       */
      public Simulator()
      {
          this(DEFAULT_DEPTH, DEFAULT_WIDTH);
          sane();
      }
  
      /**
       * Create a simulation field with the given size.
       * @param depth Depth of the field. Must be greater than zero.
       * @param width Width of the field. Must be greater than zero.
       */
      public Simulator(int depth, int width)
      {
          assert (width &gt; 0 &amp;&amp; depth &gt; 0) : 
          "The dimensions are not greater than zero.";
  
          actors = new ArrayList&lt;Actor&gt;();
          field = new Field(depth, width);
  
          // Create a view of the state of each location in the field.
          view = new SimulatorView(depth, width);
  
          factory.setupColors(view);
  
          // Setup a valid starting point.
          reset();
          sane();
      }  
  }
  </code></pre>
  
  <p>Thanks in advance</p>