I'm currently reading the OCP Study Guide. Its a great book so far.
I'm reporting this issue as I've read the
current errata and didn't see anything related.
In Chapter 2, Review questions, p101, question #18:
Which of the following statements about inheritance and object composition are correct? (Choose all that apply.)
...
E. Object composition requires a class variable to be declared public or accessible from a public method to be used by a class in a different package.
...
According to the answer (p.553) this option is correct because:
object composition has no notion of inheritance and variables must be exposed publically if they are to be used by other classes in different packages
I think this option is not correct as it is worded, since you can still access a protected variable trough multiple means from a class in a different package. Even from the composed instance. E.g.: through inheritance:
Moreover, even if the variable were private or package default, you could still keep a reference to the variable in the subclass constructor and cache it for later use:
Similarly you could also use a variable that has been passed to our composed class' constructor by keeping a reference to it:
This last example might seem far-fetched but please be aware that in this chapter the reader has been conditioned to look for this kind of subtleties. For instance, we have seen similar problems with collections being passed in the constructor to "immutable" objects and still being mutable from references cached outside (question #7 options E and G); we have also seen "singletons" that were not such due to the object being public or thread-unsafe getInstance implementations (question #9 options D and F).
In my opinion, the problem with option E is that it is assumed that:
Composition and inheritance are exclusive (which they are not).Every variable mentioned in the question is a "component" variable passed as a dependency to this class and not any other variable that the class or any of its superclasses might already have.You need to access the "component" instances through the composed class.You have no means to access the components from outside of the composed class' package.
So I find this option very ambiguous and context-lacking. It is frustrating because when it comes to what is possible or not, real questions in the exam are full of trickery and hacks to mislead the student!