which is true about the classes described above? A. Class A is tightly encapsulated. B. Class B is tightly encapsulated. C. Classes A and B are both tightly encapsulated. D. Neither class A nor class B is tightly encapsulated.
The answer is obviously D, but my question is regarding the explanation and the concept in general:
At first glance class B appears to be encapsulated...
Well, the principle of encapsulation says that the class should have all of its state private while providing public getters and setters to access and modify the state. There is only a setter in B, so would we still consider it being tightly encapsulated if foo variable in A were declared private?
Yes, if the variable in class A had been declared private then the subclass B would be tightly encapsulated. Encapsulation requires member variables to be declared private. There is no requirement to add getter and setter methods if they are not needed for your application.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Wait... something's weird here... Was that Dan making a post ?? Wow! Hey Dan! How've ya been?
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joined: Jul 02, 2002
Yes, it's me! I've been fine. I wish I could offer a good excuse for my disappearance, but I can't. There was no alien abduction or anything interesting like that. I can't even claim a hostage situation. Last spring, I went on vacation and just didn't get back into ranching when I returned. Judging by your posts, it appears that you have been very busy and doing well here at the Ranch. I'll try not to make a stranger of myself in the future. It's nice to be back.