| Author |
K&B encapsulation concept
|
Vad Fogel
Ranch Hand
Joined: Aug 25, 2003
Posts: 504
|
|
Hi all,
given the following: 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?
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
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>
|
 |
Vad Fogel
Ranch Hand
Joined: Aug 25, 2003
Posts: 504
|
|
|
Thanks Dan, this really helps clear the concept.
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8717
|
|
Wait... something's weird here... Was that Dan making a post ?? Wow! Hey Dan! How've ya been? -Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
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.
|
 |
 |
|
|
subject: K&B encapsulation concept
|
|
|