Updated April 1, 2023
Introduction to Coupling in Java
Coupling in JAVA is an indicator of a. dependence of a class with the other class in an object-oriented environment, b. level of flexibility the developer has in changing the code across various classes to meet the end user’s requirement, c. the way functionality of a class is used by the other class: directly or with the help of external interfaces, d. the efforts required in maintaining code post-go-live, e. the way innovative software techniques like Inversion of Control and Dependency Injection are used to inject more flexibility in coding and testing of codes.
Types of Coupling in Java
There are two major couplings in Java, and let us analyze them in detail.
1. Tight Coupling
In Object-oriented application design, there is always a need to utilize the logic developed in a class in some other class to reuse the efforts already invested and avoid reinventing the wheel.
Direct Collaboration between the classes leads to tight coupling, and its features are
- The business logic available in the called class is created as an object inside the calling class.
- The object thus created is executed within the calling class to achieve the required functionality. Hence the calling program knows the full functionality of the business logic available in the called class.
- Any changes in the business logic coded in the called class impact the results of the called class
- If the changes are inevitable in the called class, suitable changes will also have to be carried out in the calling class.
- Hence Classes are highly inter-dependent on one another
Example of Tight Coupling
Two collaborating classes in this example, “Ordervalue” and “order,” are interdependent. Calling class “Ordervalue” knows the business logic coded in the called class “order” accordingly, the code in the calling class is structured, and any change in the called class will upset the results of the program.
Hence it can be concluded that Classes “Ordervalue” and “Order” are tightly coupled.
Code:
// Tight Coupling - Sample program
public class ordervalue // Calling class
{
public static void main(String args[]) // method in the class
{
order b = new order(600,5); // creating object for the called class
System.out.println("value of the order is");
// order and execute it
System.out.println(b.value); // Prints the order value computed by
} // the called class
}
class order // Called class
{
public int value; // method in the class
order(int orderquantity, int rate) // business logic
{
this.value = orderquantity * rate; // computing the value
}
}
Output:
2. Loose Coupling
In this concept, classes that need to be collaborated to share business logic and common functionalities in OOPS are coupled through external sources. Thus, they are loosely or indirectly connected together.
Major attributes of loose coupling are
- Dependency between Classes, Code components, and modules are low.
- If the dependency is inevitable, then they are connected through external components like interfaces.
- Connectivity using interfaces is kept minimal to reap the benefits of loose coupling.
- There will not be any need to define objects around the other classes within a class, and objects will be independent.
- Each class will know little information about the other classes in this coupling. At best, each class will be aware of the interfaces the other modules are exposed to.
- Any change in the code in a class will not impact the other classes, and they don’t have to be updated.
- It provides perfect flexibility to the developer to change the code easily and accommodate new business changes.
- Time and efforts to maintain programs is reduced drastically
- Concepts of Spring framework like Inversion of control and Dependency injection are being used to overcome tight coupling.
Inversion of Control (IOC)
It is a concept by which the control of program modules or objects is transferred to the container framework. This concept is used in OOPS quite regularly. Instead of program codes making calls to a library, the container framework takes over the control and call codes. Dependency is injected into the objects as against objects creating dependencies.
This concept facilitates loose coupling and modularity in programming.
Dependency Injection (DI)
DI is the vehicle through which IOC concepts are put into use, and control transfer takes place in setting up object dependencies.
Examples of Loose Coupling
In the example, three Classes, “month1”, “month2”, “month3” are independent modules, and they collaborate little with each other through an interface “iface”. As a result, these classes have very little knowledge of the other classes on what they are doing. They only know that all the classes are interacting with an interface.
There is no object created using the other classes in any of these classes, and they are the typical examples of loose coupling.
Code:
// Loose Coupling in JAVA - Sample Program
interface Iface //Interface is defined
{
public void monthname(); //module within the interface
}
class month1 implements Iface { // Class interacts through
public void monthname() // interface
{
System.out.println("January");
}
}
class month2 implements Iface { // Class interacts through
public void monthname() // interface
{
System.out.println("Feburary");
}
}
class month3 implements Iface { // Class interacts through
public void monthname() // interface
{
System.out.println("March");
}
}
public class Subject { // Execution starts here
public static void main(String[] args)
{
Iface t = new month1(); // First class called thru
t.monthname(); // interface
Iface tx = new month2(); // Second class called thru
tx.monthname(); // interface
Iface tx2 = new month3(); // Third class called thru
tx2.monthname(); } // interface
}
Output:
Conclusion
As far as possible, applications will have to be designed to hold only loose couplings for better maintainability and serviceability and keep interdependence between program components very minimal. However, if interdependence is a must, the components will have to be connected only through the interface.
Recommended Articles
This is a guide to Coupling in Java. Here we discuss two types of coupling, tight and loose coupling in java, with its detailed explanation and examples with code implementation. You may also have a look at the following articles to learn more –