Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
Java is an Object-oriented programming language. Coupling in Java plays an important role when you work with Java Classes and Objects. It basically refers to the extent of knowledge one class knows about the other class. So in this article, you will learn all about coupling in java, its various types along with the examples.
Below topics are covered in this tutorial:
Let’s begin.
A situation where an object can be used by another object is termed as coupling. It is the process of collaborating together and working for each other. It simply means that one object requires another object to complete its assigned task. It is basically the usage of an object by another object, thereby reducing the dependency between the modules. It is called as collaboration if one class calls the logic of another class.
Couling in Java is further divided into two types, namely:
Let’s understand each one of them.
Tight Coupling: It is when a group of classes are highly dependent on one another. This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class. The situation where an object creates another object for its usage, is termed as Tight Coupling. The parent object will be knowing more about the child object hence the two objects are called as tightly coupled. The dependency factor and the fact that the object cannot be changed by anybody else helps it to achieve the term, tightly coupled.
Now, let me explain the concept to you with the help of an example.
Example: Suppose you have made two classes. First class is a class called Volume, and the other class evaluates the volume of the box. Any changes that would be made in the Volume class, would be reflecting in the Box class. Hence, both the classes are interdependent on each other. This situation particularly is called as tight coupling.
Below shown code will help you in understanding the implementation process of tight coupling.
Example 1:
package tightcoupling; class Volume { public static void main(String args[]) { Box b = new Box(15, 15, 15); System.out.println(b.volume); } } class Box { public int volume; Box(int length, int width, int height) { this.volume = length * width * height; } }
Output:
3375
In the above example, you can see how the two classes are bound together and work as a team. This was a simple example of tight coupling in Java. Another example depicting the process!
Example 2:
package tightcoupling; public class Edureka { public static void main(String args[]) { A a = new A(); a.display(); } } class A { B b; public A() { b = new B(); } public void display() { System.out.println("A"); b.display(); } } class B { public B() { } public void display() { System.out.println("B"); } }
A
B
Loose Coupling: When an object gets the object to be used from external sources, we call it loose coupling. In other words, the loose coupling means that the objects are independent. A loosely coupled code reduces maintenance and efforts. This was the disadvantage of tightly coupled code that was removed by the loosely coupled code. Let’s take a look at some of the examples of loose coupling in Java.
Example 1:
package lc; class Volume { public static void main(String args[]) { Box b = new Box(25, 25, 25); System.out.println(b.getVolume()); } } final class Box { private int volume; Box(int length, int width, int height) { this.volume = length * width * height; } public int getVolume() { return volume; } }
Output:
15625
Example 2:
package losecoupling; import java.io.IOException; public class Edureka { public static void main(String args[]) throws IOException { Show b = new B(); Show c = new C(); A a = new A(b); a.display(); A a1 = new A(c); a1.display(); } } interface Show { public void display(); } class A { Show s; public A(Show s) { this.s = s; } public void display() { System.out.println("A"); s.display(); } } class B implements Show { public B() { } public void display() { System.out.println("B"); } } class C implements Show { public C() { } public void display() { System.out.println("C"); } }
A
B
A
C
Tight Coupling | Loose Coupling |
More interdependency | Less Dependency, better test-ability |
Follows GOF principles of the program to interface | Does not provide the concept of interface |
Synchronous Communication | Asynchronous Communication |
More coordination, swapping a piece of code/objects between two objects are easy | Less coordination, not easy |
With this, we come to the end of this “Coupling in Java” article. I hope you found it informative. If you wish to learn more, you can check out our other Java Blogs as well.
Now that you have understood basics of Java, check out the Java Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Coupling in Java” blog and we will get back to you as soon as possible.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co