File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B encapsulation concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B encapsulation concept" Watch "K&B encapsulation concept" New topic

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="" 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

Joined: Oct 14, 2002
Posts: 8898
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.)
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.
I agree. Here's the link:
subject: K&B encapsulation concept
jQuery in Action, 3rd edition