aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with 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 "Problem with Coupling" Watch "Problem with Coupling" New topic
Author

Problem with Coupling

Richa Garg
Greenhorn

Joined: May 28, 2008
Posts: 5
Dear all,

I am having some problem in understang coupling.Please clear my doubts.
I am creating two classes that are in same package.
Variables of Class A are private but method has default access.
I am using that method in another class through its class reference variable . Are they coupled or loosely coupled or tightly coupled.


Code:

Class A{
private int a;
int getA(
return a;
)
}
Class B{
private int b;
public void get y(A a1){
b=a1.getA();
}
}
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Richa,

In your example, B is fairly tightly coupled to A.

B's behavior is dependent on A. If A#getA() changes, it affects what happens in B#gety().

At risk of confusing you further, I'd point out this example is generally acceptable under the Law of Demeter, as B#gety() is using "only one dot" of A.

But a better design in this specific example would probably be to refactor B as such:



This decouples B from A.

If B really need to consume data from A to perform its behavior, then it's fine to pass in the object and use the "only one dot" level of coupling. But in this example you give, the coupling doesn't appear to be achieving anything, Which is often the case in the sort of arbitrary examples we see when first learning!
[ May 28, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Richa ,

Can you share the source of your questions (link to online mock exams)as that would help all ,preparing for the exam and save our time looking for free online mock exams on internet.

Regards

Jolly
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with Coupling