/* Author: James Tam Version: March 5, 2021 Learning objective: types, casting, call overriden methods and methods unique to the child. */ public class Driver { public static void main (String [] args) { X anX = new X (); anX.method1(); Y anY = new Y (); anY.method1(); anY.method2(); // problem no such method in X: // anX.method2(); // problem: can't cast X to Y // ((Y) anX).method2(); anX = anY; // Problem: anX is a reference to an X //anX.method2(); // Works because anX now happens to refer to Y object, dangerous because it could also refer to an X object ((Y) anX).method2(); // Safer: check for type before casting if (anX instanceof Y) ((Y) anX).method2(); } }