Coupling refers to the degree of dependency between classes or modules in a Java application. If one class heavily depends on another, they are said to be tightly coupled. Lower coupling is preferred because it makes code more maintainable, flexible, and testable.
Types of Coupling
1. Tight coupling:
Tight coupling occurs when two classes are strongly dependent on each other. If one class knows the internal details of another class, any change in one class will directly affect the other.
Real-World Analogy: If your skin is permanently attached to your body design, changing one requires changing the other — this represents tight coupling.
class Volume
{
public static void main(String args[])
{
Box b = new Box(5,5,5);
System.out.println(b.volume);
}
}
class Box
{
public int volume;
Box(int length, int width, int height)
{
this.volume = length * width * height;
}
}
Output:
125
Explanation: In the given example, there is a strong inter-dependency between both the classes. If there is any change in Box class then they reflects in the result of Class Volume.
2. Loose coupling
Loose coupling means classes are minimally dependent on each other and communicate through interfaces or abstractions instead of concrete implementations. Frameworks like Spring achieve loose coupling using Dependency Injection (DI).
Real-World Analogy: You can change your shirt without changing your body — this represents loose coupling.
Explanation : In the above example, Topic1 and Topic2 objects are loosely coupled. It means Topic is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.
interface calVolume {
int volResult(int length, int width, int height);
}
// Box implements calVolume interface
class Box implements calVolume {
public int volResult(int length, int width, int height) {
return length * width * height;
}
}
public class Main{
public static void main(String args[]) {
calVolume calVolume = new Box();
int volume = calVolume.volResult(5, 5, 5);
System.out.println(volume);
}
}
Output:
125
Explanation : In the above program, there is no dependency between both the classes. If we change anything in the Box classes then we dont have to change anything in Volume class.
Which is better tight coupling or loose coupling?
Loose coupling is better than tight coupling because it improves flexibility, reusability, and testability. In a loosely coupled design, changes or growth in the application affect only a few components, making the system easier to maintain and scale.
Difference between tight coupling and loose coupling
Aspect | Tight Coupling | Loose Coupling |
|---|---|---|
Testability | Poor testability | Better testability |
Use of Interface | Does not use interfaces | Follows GOF principle: program to interfaces, not implementations |
Code Swapping | Difficult to swap code between classes | Easy to swap modules, objects, or components |
Flexibility / Change | Low changeability | Highly changeable and flexible |