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.
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:
And java choose the first one accessible because using morph Deer.
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.
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!
PS. What is a "morph"?