Java/J2EE and SOA (311 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (310 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is Coupling in Java and its different types?

Published on Oct 10,2019 52 Views
Aayushi Johari
A technophile who likes writing about different technologies and spreading knowledge. A technophile who likes writing about different technologies and spreading knowledge.
10 / 12 Blog from Java Strings

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Java is an Object-oriented programming language. Coupling in Java plays an important role when you work with Java Classes and Objects. It basically refers to the extent of knowledge one class knows about the other class. So in this article, you will learn all about coupling in java, its various types along with the examples.

Below topics are covered in this tutorial:

Let’s begin.

Java LogoCoupling in Java

A situation where an object can be used by another object is termed as coupling. It is the process of collaborating together and working for each other. It simply means that one object requires another object to complete its assigned task. It is basically the usage of an object by another object, thereby reducing the dependency between the modules. It is called as collaboration if one class calls the logic of another class. 

Types of Coupling

Couling in Java is further divided into two types, namely:

Let’s understand each one of them.

Tight Coupling: It is when a group of classes are highly dependent on one another. This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class. The situation where an object creates another object for its usage, is termed as Tight Coupling. The parent object will be knowing more about the child object hence the two objects are called as tightly coupled. The dependency factor and the fact that the object cannot be changed by anybody else helps it to achieve the term, tightly coupled.

Now, let me explain the concept to you with the help of an example.

Example: Suppose you have made two classes. First class is a class called Volume, and the other class evaluates the volume of the box. Any changes that would be made in the Volume class, would be reflecting in the Box class. Hence, both the classes are interdependent on each other. This situation particularly is called as tight coupling. 

Below shown code will help you in understanding the implementation process of tight coupling.

Example 1:

package tightcoupling;

class Volume {
      public static void main(String args[]) {
           Box b = new Box(15, 15, 15);
           System.out.println(b.volume);
      }
}

class Box {
      public int volume;
      Box(int length, int width, int height) {
           this.volume = length * width * height;
      }
}

Output:

3375

In the above example, you can see how the two classes are bound together and work as a team. This was a simple example of tight coupling in Java. Another example depicting the process!

Example 2:

package tightcoupling;

public class Edureka {
      public static void main(String args[]) {
           A a = new A();
           a.display();
      }
}

class A {
      B b;
      public A() {
            b = new B();
      }
      public void display() {
            System.out.println("A");
            b.display();
      }
}

class B {
       public B() {
       }
       public void display() {
             System.out.println("B");
        }
}

Output:

A
B

Loose Coupling: When an object gets the object to be used from external sources, we call it loose coupling. In other words, the loose coupling means that the objects are independent. A loosely coupled code reduces maintenance and efforts. This was the disadvantage of tightly coupled code that was removed by the loosely coupled code. Let’s take a look at some of the examples of loose coupling in Java.

Example 1:

package lc;

class Volume {
      public static void main(String args[]) {
           Box b = new Box(25, 25, 25);
           System.out.println(b.getVolume());
      }
}

final class Box {
       private int volume;
       Box(int length, int width, int height) {
             this.volume = length * width * height;
       }
       public int getVolume() {
             return volume;
       }
}

Output:

15625

 

Example 2:

package losecoupling;

import java.io.IOException;

public class Edureka {
      public static void main(String args[]) throws IOException {
           Show b = new B();
           Show c = new C();
           A a = new A(b);
           a.display();
           A a1 = new A(c);
           a1.display();
      }
}

interface Show {
      public void display();
}

class A {
      Show s;
      public A(Show s) {
           this.s = s;
      }
      public void display() {
           System.out.println("A");
           s.display();
       }
}

class B implements Show {
       public B() {
       }
       public void display() {
            System.out.println("B");
       }
}

class C implements Show {
       public C() {
       }
       public void display() {
            System.out.println("C");
       }
}

Output:

A
B
A
C

Difference between Tight Coupling and Loose Coupling

Tight CouplingLoose Coupling

More interdependency

Less Dependency, better test-ability

Follows GOF principles of the program to interface

 Does not provide the concept of interface

Synchronous Communication

Asynchronous Communication

More coordination, swapping a piece of code/objects between two objects are easy

Less coordination, not easy

With this, we come to the end of this “Coupling in Java” article. I hope you found it informative. If you wish to learn more, you can check out our other  Java Blogs as well.

Now that you have understood basics of Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Coupling in Java” blog and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.