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
- What is Loose Coupling in Java?
- Code for Loose Coupling
- Difference Between Tight Coupling and Loose Coupling
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.
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.