title: Subclasses as arguments to abstract method
body: <p>In my program I have abstract classes Animal and Flower. Animals eat Flowers, but each Animal is only supposed to eat a certain type of flower (e.g. Rhinos only eat Roses, Turtles eat Tulips).</p>
  
  <p>Right now my code basically looks like this:</p>
  
  <pre><code>public abstract class Animal {
      abstract boolean eatFlower(Flower f);
  }
  
  public class Rhino extends Animal {
      boolean eatFlower(Flower f) {
          if(!(f instanceof Rose)) return false;
          return (f.eaten = true);
      }
  }
  
  public class Turtle extends Animal {
      boolean eatFlower(Flower f) {
          if(!(f instanceof Tulip)) return false;
          return (f.eaten = true);
      }
  }
  </code></pre>
  
  <p>I'm using <code>instanceof</code> to enforce this distinction where each Animal eats only one type of Flower. I'd like to do it by changing the method signatures of Turtle and Rhino to <code>eatFlower(Tulip t)</code> and <code>eatFlower(Rose r)</code>, respectively, but I don't think this is possible in Java.</p>
  
  <p>Is there a better way than using <code>instanceof</code> to enforce this?</p>