Hi Kalpesh,
The
Java runtime resolves references to the actual class of the object when invoking methods but uses the
declared class when grabbing fields.
In your code, <code>Super sub = new Sub()</code> the
declared type of the object <code>sub</code> is <code>Super so <code>System.out.println(sub.index)</code> uses the
index field defined in the Super class.
The
actual type of the object <code>sub</code> is <code>Sub</code> so <code>sub.printVal()</code> uses the the <code>printVal()</code> defined in the <code>Sub</code> class.
Hope that helps.
------------------
Jane