Jan Stückrath wrote:Page 71:
The first code snippet seems to have a stray "final". Normally this should not be a problem, but the "equivalent ternary operator code snippet" below has no final, which makes it not really equivalent, right?
Jan Stückrath wrote:Page 179:
Last sentence of the paragraph after the big code snippet: "We are not allowed to refer to members of the Bird class since we are not in the same package and Bird is not a subclass of Bird." Did you mean "Bird is not a subclass of Goose"?
Jan Stückrath wrote:Page 249:
First sentence of last paragraph: "Any time you see a method that appears to be overridden on the example [...]". It should be "on the exam", right?
Jan Stückrath wrote:One entry for chapter 4 says "page 11", but should be 191 (it is sorted between items for page 188 and 191).
Jan Stückrath wrote:And the very last entry (for question #9 of Chapter 6 mock explanation) says "page 328" instead of 350.
Roel De Nijs wrote:
I think it should indeed be "Bird is not a subclass of Goose". We can quickly illustrate this with a code snippet, similar to the one in the book (but more concise)
Jeanne Boyarsky wrote:Can you confirm I didn't manage to mess up the copy/pasting of the accent mark in your name?
Jan Stückrath wrote:Lines 20 and 27 of the Bird class will not compile, triggered by the getText() method being overwritten in Goose. However, the casts in lines 21 and 28, which will always work, compile. Note that this problem does not occur if the subclass does not override the method, as can be seen in Line 20 of Goose (WildGoose does not override getText()). But this means that if we access a proteced method of another object of a derived class in the superclass using a non-superclass reference type, a change in the subclass can cause the superclass to not compile!
Although this behaviour makes sense if we think about how protected is meant to be used, I find it counterintuitive on first glance.
In your example we notice that both Bird and Goose classes define a protected getText() method. On lines 20 and 27 of the Bird class we try to access a member using a variable so the second bullet point definitely applies to these lines of code. According to this bullet point, protected access is allowed for references to the same class or a subclass. But the code snippet clearly illustrates that this statement is not 100% accurate and something is missing. On line 20 the type of the reference is Goose (same class as the protected method) and on line 27 the reference type is WildGoose (which is a subclass), so according to the second bullet protected access should be allowed but it isn't...
Java OCA 8 Programmer I Study Guide, Sybex wrote:A member is used through a variable. This is the case on lines 10, 11, 15, and 16. In this case, the rules for the reference type of the variable are what matter. If it is a subclass, protected access is allowed. This works for references to the same class or a subclass.
Roel De Nijs wrote:On line 20 the type of the reference is Goose (same class as the protected method) and on line 27 the reference type is WildGoose (which is a subclass), so according to the second bullet protected access should be allowed but it isn't...
Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.
Ramya Subraamanian wrote:In Goose Class method2(), shouldnt this be ok-subclass . There is no "text" in the Goose class. And "other" is able to access "text" because it inherits it from Bird.
In WildGoose method3(), shouldnt this be ok-subclass. Goose inherits "text" from Bird. And since WildGoose extends Goose it inherits "text" as well. there is no "text" in WildGoose.
The line 20 is in the Bird Class and the reference is Goose .Goose is a different class and not the same as the protected method. and when you cast it to the same class as the protected method(Bird), it works fine.
(my understanding) Line 27 doesnt compile because, though "other" is a subclass reference(of WildGoose). But it is in a different class(Bird). And so access is not permitted.
okay, thank you. understood it now.
same class" refers to the type of the reference variable, not to which instance variable text is used. So for example in method2() of class Goose, the type of the reference (local) variable is Goose => "ok - same class"
There is also a protected method getText() defined in the Goose class. On line 20 the reference variable is of type Goose, so it's the same as the class which defines the protected method
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
how do I do my own kindle-like thing - without amazonhttps://coderanch.com/t/711421/engineering/kindle-amazon