What is Coupling?

Coupling is the degree in which a class knows about the another class.

Consider the following example.

Let's assume there are 2 classes X and Y.

interface Intf {
    mtd1();
    metd2();
}

Class Y implements Intf {

}

Class X {

// Access Y methods using the Interface. It is loosely coupled.

// Access Y method directly without any interface, then it is tightly coupled.
}

In the above example, if the class X, accesses the methods mtd1() and mtd2() using the interface, then they are loosely coupled. Whereas, if X tries to access the those methods directly using Y reference, then they are tightly coupled with each other. This is because, in future if the implementation of class Y is changed, then there should be code changes to be performed in class X. Hence it is rightly coupled.

Interview Questions: 

Search