// Example // Another example to demonstrate that when you invoke a method // through an object reference, the actual class of the object // governs which implementation is used, but when you access a field, // the declared type of the reference is used. class SuperShow { public String str = "SuperStr"; public void show() { System.out.println("Super.show: " + str); } } class ExtendShow extends SuperShow { public String str = "ExtendStr"; public void show() { System.out.println("Extend.show: " + str); } public static void main( String[] args) { ExtendShow ext = new ExtendShow(); // ext is a reference that // has type ExtendShow SuperShow sup = ext; /* sup is a reference that has type SuperShow, but both ext and sup refer to the only one object that has type ExtendShow. */ sup.show(); // which method show() we call here ? ext.show(); // which show() we call here ? System.out.println("sup.str = " + sup.str); // which str we print? System.out.println("ext.str = " + ext.str); // which str we print? } }