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.

Check out our free courses related to software development.

Explore Our Software Development Free Courses

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.

Check out upGrad: Advanced Certification in DevOps

Explore our Popular Software Engineering Courses

What are the Types of Coupling?

Coupling in Java is of two types.

1) 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.

Check Out upGrad Java Bootcamp

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.

In-Demand Software Development Skills

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.

Our learners also read: Learn java free!

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.

Get Software Engineering degrees online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

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.

Read our Popular Articles related to Software Development

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’re interested to learn more about Software Development, check out Master of Science in Computer Science from LJMU which is designed for working professionals and Offers12+ Projects & Assignments, 1-ON-1 With Industry Mentors, 500+ Hours Of Learning.

What is the reason behind developers avoiding tight coupling?

Developers tend to avoid tight coupling as it reduces flexibility and makes the code inapplicable for reuse. In tight coupling, the dependence of both the classes is pretty high, which makes it difficult for developers to use it. For instance, if you wish to change the colour of your face, you need to change the colour of your body too. Developers prefer loose coupling more as the classes are independent of each other. Developers avoid tight coupling as when they need to change a part of the code they don't want to change the whole code. So they want to keep the code decoupled.

Which type of coupling is best for software engineering?

The most suitable coupling for software engineering is Data Coupling. In data coupling, the classes or elements are dependent on each other by communicating data to each other. The components are independent of each other and only pass data. These module communications don't include tramp data. An excellent example of data coupling is the customer billing system. It is better than stamp coupling as it takes the exact information it requires without the requirement of knowing the structure of the information. However, this choice can also depend upon your application.

What is the difference between coupling and cohesion?

There are several differences between cohesion and coupling. Cohesion is a concept of intra-module whereas coupling is the concept of inter-module. When there is an increase in cohesion, it is a good sign for the software, whereas an increase in coupling is mostly avoided. Cohesion depicts the functional strength of the software, but coupling depicts the independence among the modules. Cohesion should be high and coupling should be loose for the best software. In coupling, the modules are connected whereas in cohesion the module focuses on a single element.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks