This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Heres a program I wrote which I am having trouble understanding
The output is:
If the runtime type of the object is SubClass in both cases, then why when printing i, does the reference to i resolve to the SuperClass in the first case and the SubClass in the second case. Does the reference variable have some influence as it is different in both cases?
The call to methodX resolves to the SubClass in both cases as I would expect from polymorphism behavior
Polymorphism only involves instance methods. For everything else (static variables, instance variables, static methods) the only thing that matters is the type of the reference, not the type of the actual object.
All code in my posts, unless a source is explicitly mentioned, is my own.