A: how1 how1 sniff
B: how1 woof sniff
C: how1 how1 followed by an exception
D: how1 woof followed by an exception
E: Compilation fails with an error at line 14
F: Compilation fails with an error at line 15
A: An exception is thrown at runtime
B: The code compiles and runs with no output
C: Compilation fails with an error at line 8
D: Compilation fails with an error at line 9
E: Compilation fails with an error at line 12
F: Compilation fails with an error at line 13
John Lerry wrote:1) [question 8] the code is this:
I had marked the answer D and F, but the correct one is just the F.
From the time when we talk about System.out.print these are not printed on the console before it is throw the exception?
John Lerry wrote:2) [question 9] the code is this:
I had marked the answer A and D is the correct one, but the correct one is only A. The solution says that the exception is throws when the cast is done from Tree to Redwood that if I am not mistaken is the moment in which it is invoked for the second time the method "go2" inside method "go".
I actually did not really understand the operation of the cast, and especially when you can do it and when not.
Can you help me understand why in that specific case would not be possible to make the cast?
Roel De Nijs wrote:
John Lerry wrote:1) [question 8] the code is this:
I had marked the answer D and F, but the correct one is just the F.
From the time when we talk about System.out.print these are not printed on the console before it is throw the exception?
You have a compiler error at line 15! So your code won't compile, you don't have a .class file => you can't execute the program (and if you can't run the program, the program will definitely not print to the console ).
It would be another story if you didn't have a compiler error at line 15, but a ClassCastException for example. This scenario can be simulated very easy: just replace line 15 with the following line of code
Hope it helps!
Kind regards,
Roel
Roel De Nijs wrote:
When you cast, you can have 3 possible outcomes:
a compiler error (e.g. cast a Dog to a Cat; cast an Integer to a String;...) a runtime ClassCastException (e.g. this question) no compiler error and no ClassCastException
Roel De Nijs wrote:
A little code example to illustrate:
So the 1st explicit cast compiles and doesn't throw a runtime exception, because reference variable a1 refers to a Cat object and you want to cast to Cat. Cat IS-A Cat, so no problem here. The 2nd explicit cast compiles but at runtime you'll get a runtime exception, because reference variable a2 refers to a Dog object and you want to cast to Cat. Cat IS-NOT-A Dog, so you'll get a ClassCastException at runtime.
John Lerry wrote:I have a compilation error at line 15, which does not affect the operation of the cast (because I CAN make a cast from a Hound Dog because Hound IS-A Dog. Is this right?)
John Lerry wrote:but rather with the fact that once you cast a Dog is trying to access the method sniff () which is not present in the Dog class. is that correct?
John Lerry wrote:from what I understood when I try to do a cast between two types not related to each other (eg. NOT superclass and subclass) I have a compile error (so I will not even generated .class precisely because the class does not compile), but reading a your example I think it is the opposite
John Lerry wrote:in this case, trying to make a cast from Dog to Cat and you say that there is an exception (ClassCastException) and not a compilation error.
Can you help me understand?
John Lerry wrote:So I can ALWAYS make a cast from a subclass to a superclass because is satisfied the relationship IS-A and in this case we speak of upcasting. is that correct?
John Lerry wrote:I did not really understand the case instead of downcasting where, in theory, I could not make a cast from superclass to subclass as it is not respected the relationship IS-A.
How you do in that case?
John Lerry wrote:In fact, in addition to that doubt, I would have one related to cast between class and interface that I can not clear my looking at the link posted.
John Lerry wrote:I think I understand the operations of upcasting and downcasting
John Lerry wrote:The cast between class and interface works just like the cast between subclass and superclass? That is, is it sufficient to treat the interface as if it were the superclass of the class?
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|