title: Specializing method arguments in subclasses in Java
body: <p>Consider the following situation:</p>
  
  <pre><code>public abstract class AnimalFeed{
  }
  public class FishFeed extends AnimalFeed{
  }
  public class BirdFeed extends AnimalFeed{
  }
  
  public abstract class Animal{
  
  public void eat(AnimalFeed somethingToEat)
  
  }
  </code></pre>
  
  <p>Now I would like to define a class "Bird" extending "Animal" being sure that when the bird eats, it eats only BirdFeed.</p>
  
  <p>One solution would be to specify a sort of contract, in which the caller of "eat" must pass an instance of the appropriate feed</p>
  
  <pre><code>public class Bird extends Animal{
  
  @Override 
  public void eat(AnimalFeed somethingToEat){
  
      BirdFeed somethingGoodForABird
  
      if(somethingToEat.instanceOf(BirdFeed)){
      somethingGoodForABird = (BirdFeed) somethingGoodForABird
      }else{
      //throws error, complaining the caller didn't feed the bird properly
      }
  }
  }
  </code></pre>
  
  <p>Is it acceptable to delegate the responsibility of the parameter to the caller? How to force the caller to pass a specialization of the parameter? Are there alternative design solutions?</p>