×

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.


Related Topics

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Java Sort String

In this article, you will be acknowledged about how to sort a string in Java. Introduction Firstly, let us revise what is string Strings are collections of characters that are frequently used in...

4 minutes read.