File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Coupling and Cohesion in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coupling and Cohesion in java" Watch "Coupling and Cohesion in java" New topic

Coupling and Cohesion in java

Shahid Pathan

Joined: Dec 28, 2010
Posts: 23
How do we implement coupling and cohesion in java? And simple example based on real life scenario?
Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

You don't really "implement" coupling and cohesion. It's a characteristic of anything you implement.

So coupling is the extent to which different classes are dependent on each other, and so the extent to which changes to one class affect others. And cohesion is the extent to which a class has a specific purpose.
Shahid Pathan

Joined: Dec 28, 2010
Posts: 23
Thanks Matthew Brown ,
I know that this is default characteristic but we have to take care of it when we design the classes,
so that our application should be more flexible more independent on other module.
I need a basic and simple example on real life (daily life activity) and design the classes to demonstrate coupling and cohesion.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46424
You want a basic example to avoid coupling and demonstrate cohesion.Now you see the Bank class has direct access to the Account class' inner workings. Not only does that mean the security and integrity of the Account class may be breached, but also any changes to the Account class will "break" the Bank class too.

That is an example of tight coupling. It is also poor cohesion, because the Account class does not work out how much money it contains. The account now contains 123.45, irrespective of what it contained before. Using deposit and withdraw methods is far better. Oddly enough, I wrote a withdraw method earlier today: here. All the calculations are done inside the withdraw method there.
I agree. Here's the link:
subject: Coupling and Cohesion in java
It's not a secret anymore!