Alan Blass wrote:
My question is: What is the purpose of writing
TestPoly t1 = new Test1();
as oppose to Test1 t3 = new Test1();
since both give the same result?
Stephan's link answers the question "Why declare as the parent type rather than the child type?" Using the reference type TestPoly (the parent) rather than Test1 (the child) allows for polymorphism; you can introduce new kinds of TestPoly (Test3, Test4, Whatever) without changing this code.
The flipside of the issue is the answer to the question "Why declare as the child type rather than the parent type?" You would want to declare the type as Test1 (the child) rather than as TestPoly (the parent) if you needed to invoke a method supported by that specific child but not by every TestPoly. For example, if Test1 included not only the inherited hello() and the overridden print() methods but also its own doTest1Stuff() method, then you would be unable to invoke the latter method without casting if the declared type were TestPoly.