title: Type (Dynamic binding) in java
body: <p>Vegetable Class</p>
  
  <pre><code>  public class Vegetable {
       public void cook() {
       System.out.println("cook veg");
       }
       public static void eat(Vegetable veg) {
       System.out.println("eat veg");
       }
       public static void eat(Bean bean) {
       System.out.println("eat bean");
       }
       public void pick(Vegetable veg) {
       System.out.println("pick 1");
       }
   public void pick(Bean bean) {
   System.out.println("pick 2");
   }
  } // end class Vegetable
  </code></pre>
  
  <p>Bean Class</p>
  
  <pre><code>public class Bean extends Vegetable {
   public void cook() {
   System.out.println("cook bean");
   }
   public String name() {
   return "bean";
   }
  } // end class Bean
  </code></pre>
  
  <p>Lentil Class</p>
  
  <pre><code>public class Lentil extends Bean {
   public String name() {
   return super.name()+" lentil";
   }
   public void pick(Vegetable veg) {
   System.out.println("pick 3");
   }
  } // end class Lentil
  </code></pre>
  
  <p>Testing Class</p>
  
  <pre><code>public class Test{
      public static void main(String[] args){
      Vegetable v = new Vegetable();
      Bean b = new Bean();
      Lentil len = new Lentil();
      Vegetable vb = new Bean();
      System.out.println(v.pick(len));
      System.out.println(b.pick(len));
      System.out.println(len.pick(len));
      System.out.println(Vegetable.eat(len));
      }
  }
  </code></pre>
  
  <p>I really don't understand why these work. For example v.pick(len). len is declared as a Lentil and is of type Lentil. So how will this work if there is no function that takes a Lentil? Same goes for all of the other functions, I really don't understand how Lentil works.</p>