Given: Sour extends Blue extends Pan
Pan p4 = new Sour();
Then set of 6 new statements given and asked to choose which ones will compile. They are:
Pan p5 = p4;
Pan p6 = (Blue)p4;
Blue b2 = (Blue)b4;
Blue b3 = (Sour)p4;
Sour s1 = (Blue)p4;
Sour s2 = (Sour)p4;
Answer: 5th one won't compile. Rest all will compile and run without exception.
Doubt: As a beginner I have understood that LHS should be usually a super class and RHS should be subclass if downcast not required like shown in original statement Pan p4 = new Sour();
I want to further understand on what basis the options are correct or wrong. For instance (1) is correct since p4's object type is still a sub of Pan so assignement is valid. I did not understand for rest. What is checked at compile time and runtime to consider them valid. As in what is matched with what for compile and runtime?
Thanks.