What are the differences between tight coupling and loose coupling in Java?



In object-oriented programming, coupling is a term used to describe the level of dependency each class or component of an application has on another.

Tight coupling in Java means features of different classes and objects have high dependence on one another, whereas Loose coupling means components have very low or no dependency on one another.

This article explains how tight coupling is different from loose coupling in Java.

Tight Coupling in Java

In tight coupling, if the implementation of one class changes, the dependent class might also need to be changed. It makes Java code less flexible and more difficult to maintain, especially in large-scale applications.

Identifying the changes in small applications is very easy and also, there is less chance of missing anything. But in large applications, these interdependencies are not always known by every programmer.

Example

Following Java program is an example of tight coupling:

class A {
   public int a = 0;
   public int getA() {
      System.out.println("getA() method");
      return a;
   }
   public void setA(int aa) {
      if(!(aa > 10))
         a = aa;
   }
}
public class B {
   public static void main(String[] args) {
      A aObject = new A();
	  // Not suppose to happen as defined by class A, this causes tight coupling
      aObject.a = 100; 
      System.out.println("aObject.a value is: " + aObject.a);
   }
}

The output of the above Java program is as follows:

aObject.a value is: 100

The above example uses tight coupling. The class B knows about the details of class A. If class A changes the variable 'a' to private, then class B breaks. Also, class A's implementation states that variable 'a' should not be more than 10, but as we can see, there is no way to enforce such a rule, as we can go directly to the variable and change its state to whatever value we decide.

Loose Coupling

Loose coupling is a design goal to reduce the inter-dependencies between components of a system. It reduces the risk of a change in one component will require changes in any other component. It is used with the intention to increase the flexibility of the application, make it more maintainable and stable.

Example

The Java program given below is an example of loose coupling.

class A {
   private int a = 0;
   public int getA() {
      System.out.println("getA() method");
      return a;
   }
   public void setA(int aa) {
      if(!(aa > 10))
         a = aa;
   }
}
public class B {
   public static void main(String[] args) {
      A aObject = new A();
	  // No way to set 'a' to such value as this method call will
      // fail due to its enforced rule.
      aObject.setA(100); 
      System.out.println("aObject value is: " + aObject.getA());
   }
}

The above code gives the following result:

getA() method
aObject value is: 0

In the above example, the code implementation uses loose coupling and is recommended. If class A is changed internally, class B will not break, as it uses only class A as a way of communication.

Tight Coupling VS Loose Coupling

Let's compare and discuss the difference between tight and loose coupling in Java:

Tight Coupling

Loose Coupling

In tight coupling, one class is highly dependent on another class's internal implementation.

In loose coupling, classes communicate through separate interfaces.

It makes your Java code less flexible and harder to maintain.

It makes your Java code more flexible and easier to maintain and reuse.

A change in one class often requires changes in dependent classes.

Most of the time, changes in one class do not affect other classes.

Users can see internal details.

Encapsulation is used, which hides the internal logic.

Updated on: 2025-05-20T18:33:46+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements