Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts self test Chapter 2 [K&B7]

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some doubts about the exercises in Chapter 2:
1) [question 8] the code is this:


as a result there are these possibilities:

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


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?


2) [question 9] the code is this:


as a result there are these possibilities:

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


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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

In this question you can't have both A and D as correct answers! For the simple reason: either the program compiles without errors and then you can run the program; or the program fails to compile and you can't run the program.

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


  • Don't worry if you have some trouble understanding casting! Casting is one of the topics about which most questions are asked here on the ranch. It's hard to explain all rules of casting in a single post. It would require several pages in a study guide to do so. It's quite a complicated topic and if you search this forum (and the OCPJP one) you will find plenty of topics discussing doubts about casting.

    These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls):
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class


  • Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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


    I would understand if I got it right. 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?) 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
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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



  • I read one of your example but I can not understand.
    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:

    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.


    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?

     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?)

    Correct! Because Hound and Dog belong to the same class hierarchy (Hound IS-A Dog) you can make a cast from Hound to Dog.

    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?

    Spot-on! This line of code ((Dog) new Hound()).sniff(); is exactly the same asSo you are trying to invoke the sniff method on a Dog reference variable, but the Dog class only has a bark method. Hence you'll get a compiler error!
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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

    You can only cast between 2 classes if they are in the same class hierarchy (IS-A returns true). With an interface and a class it's another story (and it depends if your class is final or not, as you probably read in one of the links I mentioned in a previous post).

    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?

    One very important thing you should always remember: the compiler doesn't execute code, so it doesn't care that reference variable a1 is refering to a Cat object. The only thing the compiler knows is the type of reference variable a1: it's an Animal. And of course the same applies for reference variable a2: the compiler has no interest at all in the actual object, it just knows one thing (and doesn't want to know more): the type of a2 is Animal. The compiler is not at all interested in the type of the actual object. This is really, really, really important!

    Back to the example now: Cat IS-A Animal, so that's why the compiler allows you to cast reference variable a2 to Cat. So it compiles without errors, but at runtime this cast will fail and a ClassCastException is thrown because the type of the actual object (Dog) and the type used in the cast (Cat) are not compatible with each other.

    Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?

    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?

    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.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?

    Yes! And the cast happens implicitly, so you may (or may not) add the cast yourself. Both cast statements compile and are equivalent:

    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?

    Let me try again, because my explanation seems not 100% clear. ClassX and ClassY belong to the same class hierarchy if they pass the IS-A test, meaning either ClassX IS-A ClassY or ClassY IS-A ClassX returns true. When you downcast, you always have to add the cast yourself, so you can only use an explicit cast. And be careful, it's a risky operation: if the actual object and the cast type are not compatible, you'll get a ClassCastException (which is a runtime exception). A few examples to illustrate:

    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.

    Let us first try to have a good understanding of casting with classes before we add interfaces to this great story. Once you understand the above, you can share your doubt(s)/question(s) about casting using class and interface.
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think I understand the operations of upcasting and downcasting and so now I have a question.
    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?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Lerry wrote:I think I understand the operations of upcasting and downcasting

    Excellent!

    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?

    Not at all! Read this post very carefully, it contains everything you need to know about casting using a class and an interface.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic