I'm looking at some past exam papers for OOP and I would appreciate any help with understanding the following code. The question is, given this first block of code and that Sandwich implements Edible, which of the following statements are legal?
Sandwich sub = new Sandwich();
Rectangle cerealBox = new Rectangle(20,30,20,10);
Edible e = null;
e = sub;
sub = e;
sub = (Sandwich) e;
sub = (Sandwich) cerealBox;
e = cerealBox;
e = (Edible) cerealBox;
e = (Rectangle) cerealBox;
e = (Rectangle) null;
e = (Edible) sub;
cerealBox = (Rectangle) new Object();
My current understanding is that the first statement is true because a sub has the required elements to make up an edible Object, hence it does not work the other way round for the second statement. And with the third statement casting does allow this to work. But the fourth doesn't because cerealBox doesn't apply to Sandwich. Then the last two do work because of the casting. But apparently the 6th one works?
Sorry about my terrible explanation of what I know, any help would be appreciated.