This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The first output line is �BASE� because base is a Base type reference variable referring to an instance of Base. So �System.out.println("BASE");� is executed. The next line prints an exception occurred on line 3. At this point the reference variable base is referring to a base instance, so this cast is not valid. A super class instance is not the same of a sub class instance.
The first line is �DERIVED� because base is referring to an instance of Derived. So line 2 makes a polymorphic call to show() and �System.out.println("DERIVED");� is executed. The second one is �DERIVED� because the same overrided version of show() is executed on line 4. Note that the cast on line 3 is valid because the instance referenced by base IS A instance of DERIVED.