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 OO Concepts - Encapsulation, Cohesion, Coupling 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 "OO Concepts - Encapsulation, Cohesion, Coupling" Watch "OO Concepts - Encapsulation, Cohesion, Coupling" New topic

OO Concepts - Encapsulation, Cohesion, Coupling

Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

Dieter Quickfend wrote:You're the one who asked for an example of both at the same time, perhaps it's convoluted, but I'm not planning on writing a novel.

More importantly, (and that was the purpose) it shows that bad encapsulation does not necessarily mean tight coupling. If you've got public variables that nobody uses, you've got bad encapsulation and loose coupling.

So in most cases, bad encapsulation enables tight coupling, but not in all cases, it is not a given.

And it is tight coupling that directly represents the problem here, so the solution should definitely be tight coupling.

its all a mater of definition.
By strong encapsulation we mean the "allowance" or "enforcement"? (EXISTS vs FORALL)
By loose coupling we mean the "allowance" or "enforcement"?(EXISTS vs FORALL)

If two perfectly encapsulated classes use one another directly (without the use of polymorphism based on interfaces) are
supposed to be tightly or loosely coupled?

After we answer all those philosophical questions (which should have been answered in the SB textbook either way) then we can start answering other questions.

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Dieter Quickfend wrote:So the solution should definitely be tight coupling.

Ikpefua wrote:

Hello Dieter do you mean solution to my question or solution to your code? because the problem with your code as it stands is poor encapsulation(public instance variables - tight coupling or vulnerable to tight coupling depending on the user), hence good encapsulation(private instance variables - loose coupling NO vulnerability) takes care of that, dont you think so?.

In Your Pursuit Towards Certification, NEVER Give Up.
Maitri Solanki

Joined: May 11, 2011
Posts: 18
Same as Nidhi, I keep on tripping upon questions of Encapsulations,Coupling and Cohesion. Being more specific I am not able to identify the examples of tight or loose coupling. I understand the concept that "coupling is the degree that one class knows about another."
Here is one I failed to answer correctly in the K&B MasterExam:

Given a class House, which are true? [choose all the apply.]

A. House has-a Roof would be considered tight coupling.
B. A turnOnStereo() method would NOT be considered cohesive.
C. House is-a Dwelling would be considered good encapsulation.
D. House has-a Door would be considered low coupling.
E. A getAddress() method would NOT be considered cohesive.

I am correctly able to identify that C and E are wrong and B is correct. I am confused about A and D. Can anyone please explain?

Preparing for SCJP 6
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
Hi Guys,

Overall good discussion.

It seems however that a few times a little anger flared up. Try to remember a few things about the context of this discussion, or any discussion in these forums:

- it's illegal to discuss real exam questions.
- any questions discussed here are either from mock exams, or they are necessarily imprecise

If you're discussing a mock question, then it's ESSENTIAL that you display the exact question, in it's entirety - and mention the author.

Making good questions requires extremely precise wording, often changing a single word can radically alter the validity of a question. Of course mock question writers are human, so errors do occur, but the default orientation should be that the question is correct. I'd say that 9 times out of 10, when people are discussing mock exam questions, the question is correct.

That leaves the 1 in 10 - if, after a question has been discussed, an error is found, that's good news! The author can fix the error, and everyone in the discussion has demonstrated a certain mastery of the topic - which is the goal after all.

Chill out, have fun.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link:
subject: OO Concepts - Encapsulation, Cohesion, Coupling
It's not a secret anymore!