A reflection after a Java interview： Inheritance and polymorphism <p>The following code:</p> <pre><code>class Base{} class Agg extends Base{ public String getFields(){ String name="Agg"; return name; } } public class Avf{ public static void main(String args[]){ Base a=new Agg(); //please take a look here System.out.println(((Agg)a).getFields()); // why a needs cast to Agg? } } </code></pre> <p>My question is: why we can't replace <code>((Agg)a).getFields()</code> to <code>a.getFields()</code>? Why we need to type cast on <code>a</code>？ And I mention that <code>getFields()</code> is not defined in class <code>Base</code>, thus class <code>Agg</code> does not extend this method from its base class. But if I defined method <code>getFields()</code> in class <code>Base</code>, like:</p> <pre><code>class Base{ public String getFields(){ String name="This is from base getFields()"; return name; } } </code></pre> <p>everything would be all right. Then <strong>((Agg)a).getFields() is equivalent to a.getFields()</strong></p> <p>In the code </p> <pre><code>Base a=new Agg(); </code></pre> <p>Does this line means <code>a</code> has the reference of <code>Agg()</code> and a can invoke directly the method of class <code>Agg</code>. But why is there difference if I do not define the method <code>getFields()</code> in class <code>Base</code>? Can any one explain this to me? </p>