Java — Cohesion in Java | Code Factory

Donate : Link

WordPress Blog : Link

Applications… : Link

  • In object oriented design, cohesion refers all about how a single class is designed. Cohesion is the Object Oriented principle most closely associated with making sure that a class is designed with a single, well-focused purpose.

Lets understand the structure of high cohesive program:

class Multiply { 
int a = 5;
int b = 5;
public int mul(int a, int b)
this.a = a;
this.b = b;
return a * b;

class Display {
public static void main(String[] args)
Multiply m = new Multiply();
System.out.println(m.mul(2, 2));



Explanation : In the above image, we can see that in low cohesion only one class is responsible to execute lots of job which are not in common which reduces the chance of re-usability and maintenance. But in high cohesion there is a separate class for all the jobs to execute a specific job, which result better usability and maintenance.

Difference between high cohesion and low cohesion:

  • High cohesion is when you have a class that does a well defined job. Low cohesion is when a class does a lot of jobs that don’t have much in common.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store