Everything You Need to Know About Loose Coupling in Java

Last updated on Jul 11,2023 4.4K Views

Everything You Need to Know About Loose Coupling in Java

edureka.co

The degree of direct usage of one element by another element is called coupling in object-oriented design. In this article, we will understand Loose Coupling in Java in the following order:

 

Types of Coupling in Java

There are two types of coupling:

In this article, we will look into the loose coupling in Java and see how to implement it.

What is Loose Coupling in Java?

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.

Imagine there are two classes A and B. Class A has only a little information about class B that was exposed through the interface, then the class A and B are said to be Loosely Coupled.

 

Code for Loose Coupling:

import java.io.IOException;
interface Animal {
   public void display();
}
class Dog {
     Animal s;
   public Dog(Animal s){
      this.s = s;
   }
   public void display(){
      System.out.println("Dog");
      s.display();
   }
}
class Cat implements Animal {
   public Cat(){}
   public void display(){
      System.out.println("Cat");
   }
}
class Cow implements Animal {
   public Cow(){}
   public void display(){
      System.out.println("Cow");
   }
}
public class Test {
   public static void main(String args[]) throws IOException {
      Animal b = new Cat();
      Animal c = new Cow();
      Dog a = new Dog(b);
      //a.display() will print dog and cat
      a.display();
      Dog a1 = new Dog(c);
      //a.display() will print dog and Cow
      a1.display();
   }
}

OUTPUT:

EXPLANATION:

All 3 classes in the above code are loosely coupled. It means that animal interface can be used to provide services to the ben user by injecting any of the implemented classes.

 

Difference Between Tight Coupling and Loose Coupling

 

In conclusion, Tight coupling is much worse as compared to loose coupling as it reduces flexibility and reusability of code, making changes is also very difficult in tight coupling. All the drawbacks of tight coupling and removed in loose coupling.

With this, we come to an end of this Loose Coupling In Java article. I hope you got an idea of how coupling works and what is Loose Coupling.

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 “Loose Coupling In Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES