Brian
Which, inserted at "//insert code", will NOT compile?(Choose all that apply.)
A.Bongo b4=b2
B.Bongo b5=(Bongo)b2
C.Bango b6=b3
D.Bango b7=(Bango)b3
E.Bingo b8=b3
F.Bingo b9=(Bingo)b3
G.Bango b10=(Bongo)b3
Answer: A and E will not compile.
For one, I guess I don't understand how G can work.
Secondly, Is there a shortcut or some other way to think of it that may help me wrap my mind around the concept?
Technically, all of the answers are correct since the question asks which WILL NOT COMPILE, and the semi-colon is missing for all of them
G doesn't work, but it does COMPILE (as Nik has said).
Simply put, anything with an explicit cast will COMPILE (but may throw an exception at runtime) - provided the cast type and the reference type are in the same hierarchy. So B, D, F and G all compile.
If the reference type of the object being assigned does not match or is not a subclass of the reference type being assigned to, it will not compile without a cast. So A (assigning a Bango to a Bongo) and E (assigning a Bango to a Bingo) do not compile, as they are assigning a superclass to a subclass.