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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Everything You Need to Know About Loose Coupling in Java

Last updated on Jul 11,2023 4.5K Views

11 / 12 Blog from Java Strings

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:

  • Tight Coupling: When an object creates the object to be used, it is called tight coupling.
  • Loose Coupling: When an object gets the object to be used from external sources, we call it loose 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.

Loose Coupling in Java

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

  • The loose coupling has better test-ability than tight coupling.

  • Loose coupling follows GOF principles of the program to interface and not implements whereas tight coupling does not provide the concept of interface.

  • It easy to swap a piece of code/objects between two classes in loose coupling whereas it isn’t that easy in tight coupling

  • Loose coupling is very changeable whereas tight coupling isn’t.

 

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 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Everything You Need to Know About Loose Coupling in Java

edureka.co