title: Accessing subclass members through super class reference variables
body: <p>I am reading a Java text book and came across a doubt.
  A reference variable of a superclass can be assigned a reference to an object of any subclass derived from that superclass. However, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass.
  Example:</p>
  
  <pre><code>class X {
      int a;
      X(int i) {
          a = i; 
      }
  }
  
  class Y extends X {
      int b;
      Y(int i, int j) {
          super(j);
          b = i;
      }
  }
  
  class SupSubRef {
      public static void main(String args[]) {
          X x = new X(10);
          X x2;
          Y y = new Y(5, 6);
          x2 = x; // OK, both of same type
          System.out.println("x2.a: " + x2.a);
          x2 = y; // still Ok because Y is derived from X
          System.out.println("x2.a: " + x2.a);
          // X references know only about X members
          x2.a = 19; // OK
          // x2.b = 27; // Error, X doesn't have a b member
      }
  }
  </code></pre>
  
  <p>So, in the above example, x2 (a variable of the superclass type) can refer to an object of the derived class, bit it cannot access subclass specific members. However, in the discussion on method overriding, it is shown that a superclass reference variable's call to an overridden method can be resolved to the subclass method. But the subclass method is not defined in the superclass, so isn't this a contradiction, how is the superclass reference variable able to access the subclass specific method? Example:</p>
  
  <pre><code>class Sup {
      void who() {
          System.out.println("who() in Sup");
      }
  }
  class Sub1 extends Sup {
      void who() {
          System.out.println("who() in Sub1");
      }
  }
  class Sub2 extends Sup {
       void who() {
           System.out.println("who() in Sub2");
       }
  }
  
  class DynDispDemo {
      public static void main(String args[]) {
          Sup superOb = new Sup();
          Sub1 subOb1 = new Sub1();
          Sub2 subOb2 = new Sub2();
          Sup supRef;
          supRef = superOb;
          supRef.who();
          supRef = subOb1;
          supRef.who();
          supRef = subOb2;
          supRef.who();
      }
  }
  </code></pre>
  
  <p>The output from the program is shown here:</p>
  
  <pre><code>who() in Sup
  who() in Sub1
  who() in Sub2
  </code></pre>
  
  <p>So how is supRef able to access the who method in the subclass object?</p>