title: Superclass reference to subclass object showing same behaviour as subclass reference to subclass object
body: <p>The following code in java, when run on elipse, gives same output even if we replace</p>
  
  <pre><code>superclass s=new sub();
  </code></pre>
  
  <p>with,</p>
  
  <pre><code>sub s= new sub();
  </code></pre>
  
  <p>Note that we have overridden methods.</p>
  
  <p>Output is:</p>
  
  <pre><code>changed supermethod in sub class
  num is sub class 5
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>public class superclass {
      int num=2;
      public static void main(String str[]){
          superclass s=new sub();
          //HERE: nothing changes if we write, sub s=new sub();
          s.supermethod();
          s.method();
      }
      void supermethod(){
          System.out.println("supermethod as in superclass");
      }
      void method(){
          System.out.println("num in superclass "+num);
      }
  }
  class sub extends superclass{
      int num=5;
      void method(){
          System.out.println("num is sub class "+num);
      }
      void supermethod(){
          System.out.println("changed supermethod in sub class");
      }
  }
  </code></pre>
  
  <p>Please point out, what are the differences in creating a sub class object in these two ways. And will there be any difference in accessing methods and variables? 
  (our java teacher says, accessing method and variables will be different in both cases)</p>
  
  <p>Also, what happens to the static methods, like main. Tough i know it is inheritable, but can someone highlight its behavior in sub classes?</p>