Forums Register Login

wiley - oca java 8 programmer 1 two intro questions (Java OCA 8 Programmer I Study Guide, Sybex)

+Pie Number of slices to send: Send
I got the following two comments on the errata page. I'm replying here so I can be more verbose and in case there is any more discussion about it.

Page xxxvi Introduction
Question 12, Answer B is incorrect. It is a private variable.


B is correct. A private instance variable automatically gets initialized to the default value for the data type. In the case of a String, this is null. This is true of all instance variables. Their access modifier is not relevant to how they are initialized. And since the main method is in the same class, it is allowed to access private variables.


Oracle Certified Associate Jave SE 8 Programmer 1:
Page xxxiv Introduction, Answer to Question 8 is incorrect. HasHorns method is Private and cannot be accessed by the Parent class. Can I trust the rest of this book?


Same deal. The hasHorns() method is a private method in the Deer class. This means that only code in the Deer class can access it. Line 7 is in the main method of the Deer class and can therefore access this method. The subclass Reindeer also has a hasHorns() method. This method is not inherited since the subclass can't see the private method in the superclass.

So in both these cases, the book is correct and you can trust it . We are logging everything incorrect (no matter how minor) on our errata page. So far there are two mock questions with incorrect answers and four typos that are wrong. Everything else is minor/obvious. And if you aren't sure of something you read, feel free to post a question in this here forum to inquire. Either someone will clarify your understanding or we will confirm it is an error.
+Pie Number of slices to send: Send
Hello for all, on the question about method hasHorns(), i.e. number 8 of the assessment test in the Sybex OCA SE 8 book pag xxxiv,
I have some problem trying to justify the correct choice.

In my mind the actual object is of type Reindeer, so I think that conceptually and actually the private hasHorns methods doesn't exists in that object and should never be used, even if the morph of the object is Deer.

But, the code works, so maybe the correct way to think can be this:

The new Reindeer() object has by itself all its members and all the members of its superclass, even the private member that, of couse, are not accessible in Reindeer's scope.
When the morph and the scope are valid, then all the accessible members inherited by the superclass has priority on the other member of the class.

Thus it's like does exists on the same level either:

  • private boolean hasHorns() {}
  • public boolean hasHorns() {}


  • And java choose the first one accessible because using morph Deer.
    +Pie Number of slices to send: Send
    PS: just would I know what do you think about
    1
    +Pie Number of slices to send: Send
     

    francisco de feudis wrote:In my mind the actual object is of type Reindeer


    Sort of. While it is a reindeer, the reference doesn't know that. It's kind of like I give you a Deer object and don't tell you what type it is. I then tell you call hasHorns(). No problem. You call the one in Deer because that's what you know about.

    If the method were public, it's kind of like you want to call hasHorns() on Deer. But you can't. Because Reindeer has hidden hasHorns() away from you.
    +Pie Number of slices to send: Send
     

    francisco de feudis wrote:In my mind the actual object is of type Reindeer, so I think that conceptually and actually the private hasHorns methods doesn't exists in that object and should never be used, even if the morph of the object is Deer.


    For the compiler the actual object doesn't matter at all! The only thing the compiler knows (and cares about) is the type of the reference variable, which is in this case Deer. And the Deer class defines a private method hasHorns(). So that means the hasHorns() method can only be used within the Deer class. Luckily the main() method is defined in the Deer class as well, so it is valid to access the private hasHorns() method. If the main() method was defined in the Reindeer class, the code would not compile and give a compiler error.
    Although the Reindeer class defines a public hasHorns() method too, this method is not executed at runtime. Because only inherited methods can be overridden and a private method is never inherited. If you would remove the private access modifier or change it to protected or public, the output will be different. In this topic you'll find an excellent explanation (with lots of code snippets) about how changing the access modifier of a method can influence which method is executed (and thus result in a different output). Definitely worth reading!

    Here is a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code. So the compiler doesn't know and care) about the actual objects, the compiler only knows about the types of the reference variables.

    Hope it helps!
    Kind regards,
    Roel

    PS. What is a "morph"?
    These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 949 times.
    Similar Threads
    Inheritance Question which method is called
    Possible errata Chapter 4 Review Questions (Java OCA 8 Programmer I Study Guide, Sybex)
    chapter 3 is the syntax for startsWith has boolean? (Java OCA 8 Programmer I Study Guide, Sybex)
    Some minor things I noticed in chapter 1 (Java OCA 8 Programmer I Study Guide, Sybex)
    JavaBeans Question 9, Chapter 4 (Java OCA 8 Programmer I Study Guide)
    OCA Java SE 8 Programmer I Study Guide: Exam 1Z0-808 by Jeanne Boyarsky & Scott Selikoff
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 02:21:24.