Loose Coupling in Java
Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable can have different types of implementation class memories. In a simple way we can, we will understand with an example:-
Interface I{
public abstract void m1();
}
class A implements I{
public void m1(){
System. out.println("This is A class m1()");
}
}
class B implements I{
public void m1(){
System. out.println("This is B class m1()");
}
}
class C implements I{
public void m1(){
System. out.println("This is C class m1()");
}
}
Class Test{
public static void main(String[]s){
I obj= new A();
obj.m1();
obj=new B();
obj.m1();
obj=new C();
obj.m1();
}
}
Explanation:-
In the above example, I have an interface like interface I; in the interface I, I have one method like void main, but if only the nature of the methods are what, here abstract public methods and there are three classes class implements I here class B implements I one more thing class C implements I, so there are three classes, and three classes implement here interface I so public void m1 method we are writing coding like a system dot out dot println this is a class m1 method. Same for classes B and C. Now here we take a class like a test class, I have one method like public static void main and writing string[] s, so here interface is I obj can have the capability to hold different implementation class memories. Hence, a is the implementation class of I; for example, here we are calling obj dot m1 method, then the compiler will go and check whether my method exists in obj type or not, so yes exists, so the compiler will go and check this method existed. Now JVM concentrates on here. We can't create an object for your interfaces, but we can create an object for implementation classes a, a is here implementation class, and I place over obj. So the method is non-static data. It's participated in the method of overriding.so definitely
Here a chance to__ and java test now the output is this a class m1 method we can understand the flexibility of loosely couple, the same reference variable can having the capability to hold that B class memory obj dot m1 method now in this particular time here we see what happen, here same reference available who obj can have B class memory then the method is executing some B class, now one more method is there one more obj equal to one more memory, obj similar to new C and obj.m1 method, currently m1 method is executing from class C., so the loose coupling is the same reference variable can having the capability to hold multiple memories like a and b and c. whatever the memory for which class memory we placing into that reference variable when will from that class memory only methods are variables are going to executing. So it's straightforward to understand.
Let's know with one real-time scenario, I A mobiles like Tata Docomo or Tata IndicomTata and one more thing is reliance related mobile's so if it is that Tata income if it is a Tata Indicom CDMA mobile, CDMA mobile we must return should we use only once, what is the Tata Indicom seen, so that mobile is tightly coupled with that a sim itself, for example, reliance mobile so that mobile is tightly coupled with sim. Still, nowadays it happened nobody that same mobile GSM mobile, so GSM mobile the same mobile can have the capability to hold our phone network can having the capacity to store data Indicom network and having the ability to hold Tata Docomo network, the same mobile can able to save ATL network, the same mobile can able to maintain idea network and Jio network. Here we see that a single mobile can have the capability to store multiple types of networks, a different type of network since it is called a loose coupling mechanism. This is a tightly coupling mechanism so tightly coupled mechanism, and another one is a loosely coupled mechanism. Here tightly coupling mechanism means one reference variable can have the capability to hold the same reference type memory but here, one reference variable can have the ability to carry a multiple implementation class memory.