title: why can't I call a subclass method using a reference of a parent type that refers to an instance of a sub-type?
body: <p>I am Learning Java, while trying to understand inheritance. I couldn't figure out why the overridden method  at the subclass walk() executed but not the other xyz() method. </p>
  
  <pre><code>   class Person{
       public void walk() { System.out.println("walking like a person "); }
     }
  
     public class Soldier  extends Person{
      @override
      public void walk() { System.out.println("marching like a  soldier "); }
      public void xyz()   { System.out.println("xyzng like a pro"); }
  
      public static void main(String[] args) {
  
          Person sp = new Soldier();
          sp.walk(); 
          sp.xyz();
       }
     }
  </code></pre>
  
  <p>Here is my question, if the following method call works fine and calls the Soldier method walk,</p>
  
  <pre><code>    sp.walk(); 
  </code></pre>
  
  <p>why the compiler complain about the this call?</p>
  
  <pre><code>    sp.xyz();
  </code></pre>