Programs

Loose Coupling vs Tight Coupling in Java: Difference Between Loose Coupling & Tight Coupling

Introduction

Java is an object-oriented programming language that uses classes and objects. Coupling plays a vital role in Java if you work with classes and objects in the Java language. Coupling refers to the degree of knowledge one element in Java has about the other element. It is the degree of usage of one class by another class. This article will give you a deep understanding of coupling in Java, its types with examples.

What is Coupling in Java?

Coupling is nothing but the dependency of one class on the other. If one object in a code uses the other object in the program, it is called loose coupling in Java. In coupling, two classes or objects collaborate and work with each other to complete a pre-defined task. It simply means that one element requires another element to complete a function. It is known as collaboration when one class calls the logic of the other class.

What are the Types of Coupling?

Coupling in Java is of two types.

Loose Coupling in Java

When two classes, modules, or components have low dependencies on each other, it is called loose coupling in Java. Loose coupling in Java means that the classes are independent of each other. The only knowledge one class has about the other class is what the other class has exposed through its interfaces in loose coupling. If a situation requires objects to be used from outside, it is termed as a loose coupling situation.

Here, the parent object is rarely using the object, and the object can be easily changed from external sources. The loose coupling in Java has the edge over tight coupling as it reduces code maintenance and efforts. A change in one class does not require changes in the other class, and two classes can work independently.

Example 1: Imagine you have created two classes, A and B, in your program. Class A is called volume, and class B evaluates the volume of a cylinder. If you change class A volume, then you are not forced to change class B. This is called loose coupling in Java. When class A requires changes in class B, then you have tight coupling.

Code

package loosecoupling;

 class Volume {

   public static void main(String args[]) {

        Cylinder b = new Cylinder(25, 25, 25);

           System.out.println(b.getVolume());

   }

}

final class Cylinder {

    private int volume;

    Cylinder(int length, int width, int height) {

             this.volume = length * width * height;

    }

    public int getVolume() {

             return volume;

    }

}

Explanation: In the above example, class A and class B are loosely coupled.

Example 2

import java.io.IOException;

interface Food {

   public void display();

}

class Italian {

  Food s;

   public Italian(Food s){

   this.s = s;

   }

   public void display(){

      System.out.println(“Italian”);

      s.display();

   }

}

class Chinese implements Food {

   public Chinese(){}

   public void display(){

      System.out.println(“Chinese”);

   }

}

class Mexican implements Food {

   public Mexican(){}

   public void display(){

      System.out.println(“Mexican”);

   }

}

public class Test {

   public static void main(String args[]) throws IOException {

   Food b = new Chinese();

   Food c = new Mexican();

   Italian a = new Italian(b);

      //a.display() will print Italian and Chinese

      a.display();

   Italian a1 = new Italian(c);

      //a.display() will print Italian and Mexican

      a1.display();

   }

}

Output

Italian

Chinese

Italian

Mexican

Explanation: In the above example, all three classes are loosely coupled. It simply means that you can use the food interface to provide services by injecting any of the implemented services.

Tight Coupling

When two classes are highly dependent on each other, it is called tight coupling. It occurs when a class takes too many responsibilities or where a change in one class requires changes in the other class. In tight coupling, an object (parent object) creates another object (child object) for its usage. If the parent object knows more about how the child object was implemented, we can say that the parent and child object are tightly coupled.

Example: Imagine you have created two classes A and B, in your program. Class A is called volume, and class B evaluates the volume of a cylinder. If you make any changes in the volume, then the same changes will reflect in class B. Hence, we can say both the classes are highly dependent on each other and are tightly coupled.

Code

package tightcoupling;

class Volume {

   public static void main(String args[]) {

        Cylinder b = new Cylinder(15, 15, 15);

           System.out.println(b.volume);

   }}

 class Cylinder {

   public int volume;

   Cylinder(int length, int width, int height) {

           this.volume = length * width * height;  }}

Output

3375

Explanation: In the above example, class A and class B are bound together and work with each other as a team. 

Differences Between Loose Coupling and Tight Coupling

The following table lists the differences between loose coupling and tight coupling.

Loose Coupling Tight Coupling
Objects are independent of each other. One object is dependent on the other object to complete a task.
Better testability Testability is not as great as the loose coupling in Java.
Asynchronous communication Synchronous communication
Less coordination. Swapping code between two classes is not easy. Provides better coordination. You can easily swap code between two objects.
No concept of interface Follows GOF principles to interface
Less information flow More information flow
Highly changeable It does not have the change capability.

Conclusion

In a nutshell, loose coupling in Java is much better as compared to tight coupling. It provides better flexibility and reusability of code. As the two classes are independent of each other, it makes changes in the code very easy. It also provides better testability. If you want to enhance your Java knowledge, check out the Executive PG Programme in Software Development course which offers 500+ hours of training and job assistance. 

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S Executive PG Programme IN SOFTWARE DEVELOPMENT
LEARN MORE

0 replies on “Loose Coupling vs Tight Coupling in Java: Difference Between Loose Coupling & Tight Coupling”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×