I'm struggling to understand one of the questions from McKenzie's
SCJA book (sorry Cameron, here we go again...)
Page 226, last question
Which of the following are valid lines of code, given the following:
Shape shape = new Rectangle (4,4);
IShape tri = new Triangle (5,3);
(...)
etc...
a) shape = tri
(...)
etc...
Option a is marked as correct.
A diagram is given a couple of pages before, where we can see that Shape is an abstract class and IShape is an interface that it implements.
My question: if tri is of type IShape, can it be assigned to shape, which is a variable of type Shape? Since Shape implements iShape, not the other way around, won't the compiler complain?
What if I later give tri a value that isn't a Shape, but still implements the interface IShape? Then shape = tri will be problematic, won't it?
Thank you for your help!