×

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

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Pyramid Program in Java

Pyramid Program in Java In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except...

2 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.