What is cohesion in Java & it’s Types with Examples

Cohesion is an object orient designing approach of java programming which is basically deals with designing of class and have an aim of single and well focused class so that it is easier to maintain and reusable for future.

While designing of class you should strive for high cohesion i.e a cohesive component/module focus on single task with little interaction with other module of system. Cohesion is Intra-module Concept.

Good designing of class refer to high cohesion while bad design refer to low cohesion.

Types of Cohesion

  • High Cohesion (Good Programming Design).

When the design of the class is good means when class is easy to update, retrieve & use than class is said to be High Cohesion. In high cohesion single method is used in one class. High cohesion leads to more usability of code.

For every component we have to maintain a clear well define functionality such type of component is said to be follow high cohesion. The classes in the subsystem perform similar task and are related to each other.

Also learn python list of dictionarylist of dictionary

  • Low Cohesion (Bad Programming Design).

When the design of the class is not good means when class is not easy to update, retrieve & use than class is said to be Low Cohesion. In low cohesion so many method is used in one class. Low cohesion leads to less usability of code.

Lots of miscellaneous and auxillary classes, no associations.

Cohesion in java

Explanation: In the above image, you can see that in low cohesion so many methods is used inside single class which result less usability of class where in high cohesion every new class is introduce for every method which result high usability and maintainability of code.

Example: High Cohesion in Java

class Multiply {
     int x = 10;
     int y = 10;
     public int mul(int a, int b)
     {
       this.x = x;
       this.y = y;
       return x * y;
     }
 }
 class Display 
{
  public static void main(String[] args)
    {
      Multiply m = new Multiply();
      System.out.println(m.mul(10, 10));
   }
 } 

Output

100

Code Explanation : Assume we have a class that multiply two numbers, but the same class creates a pop up window showing the result this is the example of low cohesive class because the window and the multiplication operation don’t have much in common.
To make it high cohesive, we would have to create a class Display and class called Multiply. The Display will call Multiply’s method to get the result and display it. This way to develop a high cohesive solution.

 class Name { 
     String name; 
     public String getName(String name) 
     { 
         this.name = name; 
         return name; 
     } 
 } 
 class Age { 
     int age; 
     public int getAge(int age) 
     { 
         this.age = age; 
         return age; 
     } 
 } 
 class Number { 
     int mobileno; 
     public int getNumber(int mobileno) 
     { 
         this.mobileno = mobileno; 
         return mobileno; 
     } 
 } 
 class Display { 
     public static void main(String[] args) 
     { 
         Name n = new Name(); 
         System.out.println(n.getName("Programiz.net")); 
         Age a = new Age(); 
         System.out.println(a.getAge(10)); 
         Number no = new Number(); 
         System.out.println(no.getNumber(1234567891)); 
     } 
 } 

Output:

Programiz.net
10
1234567891

Also Learn

If not operator in python

What is Cohesion in Java?

Cohesion is an object orient designing approach of java programming which is basically deals with designing of class.

How many Types of cohesion are available in java?

There are two types of cohesion in java High Cohesion and Low Cohesion.

What Does High Cohesion Means in Java?

When the Design of class is good means when class is easy to update, retrieve & use than class is said to be High Cohesion.

What Does Low Cohesion Means in Java?

When the design of the class is not good means when class is not easy to update, retrieve & use than class is said to be Low Cohesion

Was this article helpful?
YesNo

24 thoughts on “What is cohesion in Java & it’s Types with Examples”

Leave a Comment

Your email address will not be published. Required fields are marked *