;
Coupling in Java and its different types

Related Courses

Coupling in Java and its different types

Java happens to be an Object-oriented programming language. Coupling is quite essential in Java while you deal with Java Classes and Objects.  It generally refers to what level one class is dependent on the other class. In this article, you will learn all about coupling. Coupling is essential for all programming languages, and here you will find various examples in detail explaining what is coupling. The topics covered in this article are what is coupling in Java, various types of coupling, tight and loose coupling and the difference between these two types of coupling. You should know that coupling is the extent to which a class knows about some other class. And, It's a very important feature of any programming language. So let's begin this article. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training.

Coupling in Java

Coupling is a scenario where an object can be used by another object. It ensures collaboration and doing the task for each other.  And in a nutshell. It is the calculation that explains to what level an object needs another for completing its task. It’s the usage of an object by some other object, and thus a low coupling will mean a low level of dependency of one module over the other. We know hence couping as a collaboration of one class on the logic of another class. Hence, you can easily understand that for a better module-based programming experience the programming language needs to ensure low coupling. That means the modules are not dependent on one another and hence programmers can work on them separately without bothering about the other modules. And this is because the modules are independent of each other, as the programming language is ensuring low coupling. 

Types of coupling

We have two types of coupling in Java, and they are as follows:

  • Tight coupling

  • Low coupling

Let's have a look at each of them separately.

Tight coupling

We see tight coupling in conditions where a group of classes is extensively dependent on each other. And this happens when there are so many responsibilities in the classes, or when one liability is on various classes, and not merely one class. And this scenario when one object creates another object for its purpose is known as Tight coupling. And the parent object knows a lot about the other child object, and that’s why there is high coupling between the two. The dependency level, and the fact that anybody cannot change the object, make them support tight coupling.

Now, let's have some examples related to tight coupling. Keep in mind that Java supports both tight coupling and loose coupling. However, for good architecture, a software design should support loose coupling. It's one of the prime features of a good software design.

Example: Just think of a situation when we have two classes. The first class is the class with parameters for the area, and the second class has the methods for calculating the area of the rectangle. However, it inherits the parameters from the first class. Hence, these classes are loosely coupled to some extent as the change in one class does affect the other class to a low level however. However, suppose the first-class make use of the method for calculating the area that is defined in the second class. See below, and then you will find that the two classes are more tightly coupled, as the first class has the dependency in the form of the second class. If we remove the second class then we cannot just execute the first classes, more in second case, and to lesser extent in first case. And hence, the two classes are tightly coupled in both cases. In first case, we will not get the output that we need without the second class. And in the second case, an exception will be thrown, like the method definition missing. It's not good for the software design, and hence we make use of the interfaces. We will see that later in some in this blog. Please just keep reading this article on coupling.

Class parameters

{

     public int len=10;

     public int breath=10;

     public static void main(String[] args)

     {

           Area b=new Area();

            int a = b.area(this.len, this.breath);

           System.out.printlin(“ Area=”, +a);

      }

Class Area{

        public void area(int a, int b)

          {

                 return(a*b);

          }

}

Output:

Area = 100

Above we see that the two classes are being bound together, and they work like a one unit, and one part does not work without the other part. It’s one of the simplest examples in Java of tight coupling. Let's have one more example explaining tight coupling. And we repeat that tight coupling is not good for a proper software design.

Example 2:

 package CalculateArea; 

public class A {

      public static void main(String args[]) {

           Area x = new Area();

           x.PrintA();

      }

}

class Area {

      LenBreath y;

      public Area() {

            y = new LenBreath();

      }

      public void PrintA() {

            System.out.println("Area");

            y.PrintArea();

      }

}

class LenBreath {

       public int a;

       public int b;

       public LenBreath() {

             a=12;

             b=12;

       }

       public void PrintArea() {

             System.out.println(this.a * this.b);

        }

}

Output 

Area

144

Above we see even more tight coupling and this is not good for a good software design. Also, you can understand that different programmer in a software development team code different module. And If there is tight coupling, then they cannot work independently. Let's now see what is the loose coupling in detail. And we will find that it is better for a good software design than to

Loose coupling

When a certain object gets the object from any external source, then we term it as loose coupling. This implies that the objects are loosely coupled and are independent. If the objects are loosely coupled, then the maintenance becomes easier, and that means less effort is required. And you should know that it’s the heavy maintenance effort required, which Is the disadvantage of the tight coupling. And this has been removed through the implementation of loose coupling. Let's have a look at two code examples, explaining the loose coupling. And you will find that we make use of the interface. And if you want to learn these in detail, please join our Java training classes. It will cost you a nominal fee, and by the end, you will be a complete Java developer. Please be assured. Contact us now, or anytime.

Example 1:

package loose_coupling;

class Area {

      public static void main(String args[]) {

           Rectangle r = new Rectangle(15, 15);

           System.out.println(r.getArea());

      }

}



final class Rectangle {

       private int area;

       Rectangle(int length, int breath) {

             this.area = length * breath;

  }

       public int getArea() {

             return area;

       }

}

Output:

225

 Example 2:

package loose_coupling;

import java.io.IOException;

public class Main {

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

           PrintShape x = new Square();

           PrintShape y = new Rectangle();

           Master a = new Master(x);

           a.PrintResult();

           Master a2 = new Master(y);

           a2.PrintResult();

      }

}

interface PrintShape {

      public void Printshape();

}



class Master {

      PrintShape s;

      public Master(PrintShape s) {

           this.s = s;

      }

      public void PrintResult() {

           System.out.println("Master");

           s.Printshape();

       }

}



class Square implements PrintShape {

       public Square() {

       }

       public void Printshape() {

            System.out.println("Square");

       }

}

class Rectangle implements PrintShape {

       public Rectangle() {

       }

       public void Printshape() {

            System.out.println("Rectangle");

       }

}

Output:

Master

Square

Master

Rectangle

Explanation:

Through the use of coupling, we have ensured above the loose coupling. We see above that the Square and Rectangle classes can exist without each other. Hence, they are independent of each other. If there is no Rectangle class then as well the Square class can exist. And this has been made possible through the use of an interface. Hence, an interface concept plays an important role in ensuring the Loose coupling. And this is not true only for Java. But in fact, this true for all object-oriented programming languages.

What a tight coupling and loose coupling differ in between each other

Differences between the tight coupling and the loose coupling are as below:

  1. Tight coupling leads to more interdependency, and loose coupling leads to low dependency and it supports more test-ability.

  2. The loose coupling follows the GOF program of the interface and not the implementations. And the tight coupling does not cater to us the interface concept

  3. There is more information from tight coupling, and there is less information flow in loose coupling.

  4. There is less coordination in loose coupling and more coordination in tight coupling.

  5. It’s easier to code in the loose coupling, and quite complex to code in tight coupling.

Hence, loose coupling is always better than tight coupling. And with this, we come to the end of this tutorial on the Java coupling concept. We hope you now have understood what coupling is all about. If you will learn software project management then you will find that loose coupling is always better than tight coupling for software development. And you have seen it here as well.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let's have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.