Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

coupling in java example

Coupling in Java example

Coupling in java refers to the degree of direct knowledge that one element has of another. In other words, how often do changes in Class A force related changes in class B.
There are two types of coupling in java:

  1. Tight coupling in java: In general, Tight coupling means the two classes often change together. In other words, if A knows more than it should about the way in which B was implemented, then A and B are tightly coupled.
    Example : If you want to change the skin, you would also have to change the design of your body as well because the two are joined together – they are tightly coupled. The best example of tight coupling is RMI(Remote Method Invocation).
    // java Tight coupling Example:
    class Animal {
        Body b = new Body();
        public void startRunning()
        {
            t.running();
        }
    }
    class Body {
        public void running()
        {
            System.out.println("Body is running");
        }
    }

    Explanation: In the above program the Animal class is dependents on Body class. In the above program Animal class is tightly coupled with Body class it means if any change in the Body class requires Subject class to change. For example, if Body class running() method change to going() method then you have to change the startRunning() method will call going() method instead of calling running() method.

    // java Tight coupling Example concept
    class Thatsjavainfo
    {
         public static void main(String args[])
         {
             Thats t = new Thats(5,5,5);
             System.out.println(t.volume);
         }
    }
    class Thats
    {
         public int T;
         Thats(int length, int width, int height)
         {
             this.T = length * width * height;
         }
    }

    Explanation:In the above example, there is a strong inter-dependency between both the classes. If there is any change in Thats class then they reflects in the result of Class Thatsjavainfo.

  2. Loose coupling in java: In simple words, loose coupling means they are mostly independent. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled. In order to over come from the problems of tight coupling between objects, spring framework uses dependency injection mechanism with the help of POJO/POJI model and through dependency injection its possible to achieve loose coupling.
    Example : If you change your shirt, then you are not forced to change your body – when you can do that, then you have loose coupling. When you can’t do that, then you have tight coupling. The examples of Loose coupling are Interface, JMS.
    // java Loos coupling Example:
    public interface Body
    {
        void running();
    }
    class Body1 implements Body {
    public void running()
        {
            System.out.println("running loos");
        }
    } class Body2 implements Body {
    public void running()
        {
            System.out.println("running loos more");
        }
    } public class Animal {
    public static void main(String[] args)
        {
            Body b = new Body1();
            t.running();
        }
    }

    Explanation : In the above example, Body1 and Body2 objects are loosely coupled. It means Body is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.

    // Javaloose coupling concept
    class Thatsjavainfo
    {
         public static void main(String args[])
         {
             Thats t = new Thats(6,6,6);
             System.out.println(t.getT());
         }
    }
    final class Thats
    {
         private int T;
         Box(int length, int width, int height) 
         {
             this.T = length * width * height;
         }
         public int getT()
         {
             return T;
              
         }
    }

     
    Explanation : In the above program, there is no dependency between both the classes. If we change anything in the Thats classes then we dont have to change anything in Thatsjavainfo class.

    Which is better tight coupling or loose coupling in java?

    In general, Tight Coupling is bad in but most of the time, because it reduces flexibility and re-usability of code, it makes changes much more difficult, it impedes test ability etc. loose coupling is a better choice because A loosely coupled will help you when your application need to change or grow. If you design with loosely coupled architecture, only a few parts of the application should be affected when requirements change.
    Lets have a look on the pictorial view of tight coupling and loose coupling:

    coupling in java
    coupling in java thatsjavainfo

    Difference between tight coupling and loose coupling

    • Tight coupling is not good at the test-ability. But loose coupling improves the test ability.
    • Tight coupling does not provide the concept of interface. But loose coupling helps us follow the GOF principle of program to interfaces, not implementations.
    • In Tight coupling, it is not easy to swap the codes between two classes. But it’s much easier to swap other pieces of code/modules/objects/components in loose coupling.
    • Tight coupling does not have the changing capability. But loose coupling is highly changeable.


This post first appeared on ThatsJavaInfo - A Place To Learn Java And Its High, please read the originial post: here

Share the post

coupling in java example

×

Subscribe to Thatsjavainfo - A Place To Learn Java And Its High

Get updates delivered right to your inbox!

Thank you for your subscription

×