Does anyone have any workable strategy for solving casting/upcasting questions? I understand when upcasting and downcasting is allowed but when the questions tend to have multiple objects involved i tend to get confused pretty quickly. For example, what is the best approach to finding the answer to a question like this:
Question: What would be the result of compiling the following program:
interface Inter{}
class Base implements Inter{}
class Derived extends Base{}
class ZiggyTest2{
public static void main(String[] args){
Base b = new Base();
Derived d = new Derived();
Inter i = (Base)b;
i = (Base)d;
Derived bd = (Derived)b;
b = (Base)i;
}
}
I am not really interested in the answer but more the approach to solve the question. Is there any approach/strategy i can use to solve an upcasting/downcasting question like above? For example can the references/objects be drawn on paper so that i can get a visual representation and can that help?
Thanks