More generally, does anybody else find these object-oriented definitions questions really hard, or is it just me?
Originally posted by Burkhard Hassel:
I don't find them hard, but this one is again an example of a really silly, bad and totally ambigous question, that cannot be properly answered at all.
Where is this question from?
I just hope the ones on the real SCJP exam are better!
Originally posted by Bert Bates:
I do think that the question in question is fairly representative of what you'll find on the real exam.
We're going to admit it up front. The Sun exam's definitions for cohesion and coupling are somewhat subjective, so what we discuss in this chapter is from the perspective of the exam, and by no means The One True Word on these two OO design principles.
Tight coupling is generally a bad thing - it means that classes know more about each other than they need to or should. It's fairly defensible to say that a House class would reasonably "have-a" Roof object, so this wouldn't be considered tight coupling - House does know about Roof because it "has" a roof, but it seems natural - so A is incorrect.
I guess D is related to A - the idea being that the existence of a "reasonable has-a" doesn't create tight coupling - reasonable of course being subjective. On the real exam we tried to make up "reasonable" real-world examples.
K&B elsewhere say that "coupling refers to the degree to which one class knows about or use members of another class", and that "loose coupling describes a situation in which classes know only about each other's APIs". I'm not sure, then, how that fits in with the explanation.
Further, just knowing that "House has-a Door" tells us very little about how a House uses its Door -- it's entirely possible that House fiddles with Door's public doorKnob field rather than using get/setDoorKnob...or not, we don't know.
Finally E - i suppose this one's debatable too - the admittedly subjective argument is that in the real world it's reasonable that a House object would have an address property, and that having an address property wouldn't hurt the House classes cohesiveness.