title: How to do object.getClass().getName() in Java
body: <p>I want to implement to classes, <code>Food</code> and <code>FoodFactory</code> such that they satisfies the following input and output.</p>
  
  <pre><code>//Input:
  FoodFactory ff = new FoodFactory();
  Food f1 = ff.getFood('Fruit');
  Food f2 = ff.getFood('Meat');
  f1.serveFood(); 
  f2.serveFood();
  System.out.println(f1.getClass().getSuperClass().getName());
  System.out.println(f1.getClass().getName());
  System.out.println(f2.getClass().getName());
  
  //Output:
  I'm serving Fruit
  I'm serving Meat
  Food
  Fruit
  Meat
  </code></pre>
  
  <p>Suppose we have the following constraints:</p>
  
  <ol>
  <li>Only write the two classes <code>Food</code> and <code>FoodFactory</code></li>
  <li>Only java.util.* is allowed</li>
  </ol>
  
  <p>What I don't know is how to make <code>getClass().getName()</code> working in different situation. I can only get <code>FoodFactory</code>.</p>
  
  <p>Thanks!</p>