Campbell Ritchie wrote:In the second case, the compiler can “know” that the black ink cannot be in the List, so the cast is bound to fail.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Because the compiler only compiles the code and does not run it, it does not remember what was added to the List. It simply knows the List contains <Ink> which might be a valid candidate for the cast.
In the second case, the compiler can “know” that the black ink cannot be in the List, so the cast is bound to fail.
Tiberius Marius wrote:If inheritance relationship between those two is not possible a compiler error will result...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Chris R Barrett wrote:As I understand Henry's answer, the compiler is open to the possibility that another class extends BlackInk and implements Printable, which would make the IS-A relationship true.
Winston Gutkowski wrote:Well done Chris; cow for that.
Mike. J. Thompson wrote:Well certainly in the general case the compiler cannot know the runtime type of an object. I guess the compiler designers decided not to bother tracking the runtime type even in the specific cases when it is known.
Mike. J. Thompson wrote:Printable p = (Printable) new Object();
could be re-written as
Object o = new Object();
Printable p = (Printable) o;
The compiler could still know the cast could fail in the second case (even if there where statements between those two), but it's generally accepted that this doesn't compile.
Mike Simmons wrote:Actually, the language spec prohibits the compiler from rejecting a program based only on knowledge of the runtime type of an object. Only the declared type of the reference may be considered.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Tiberius Marius wrote:I have to ask because no one replayed on it specifically and i m not sure it's 100% right , it's my view on casting correct ? And if not what the correct version ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Tiberius Marius wrote:I have to ask because no one replayed on it specifically and i m not sure it's 100% right , it's my view on casting correct ? And if not what the correct version ?
You're basically correct; and you ARE correct if the type you're casting to is a class. But if it's an interface (as with your cast to Printable), and the declared type of the variable is not final, then the compiler assumes that any subclass of it could implement that interface.
Does it make sense now?
Winston
Chris R Barrett wrote:Hi Georges,
Welcome to the Ranch!
Hmmm... You sure the text says only D was the right answer? My book says B & D is the correct answer.
And D, as written in her question, is correct and will cause a compile time failure.
My question to you is why? Look closely at the code used in answer D.
(With only 107 seconds per question, it's easy to skim over such differences and not notice them. It's important you train your mind as the exam will expect you to spot subtle changes right away).