What is loose coupling and tight coupling in oop (java)

I have some confusion regarding loose connection and hard connection in java.as I know that loose connections are associated with the least information about each other and tightly related dependencies of the means. We know that loose communication can be achieved through the implementation of an interface, and inheritance makes a tough couple.

eg:

1) A (interface) <--------------------- B (class)

2) C (class) <--------------------- D (class)

suppose these four classes are part of my entire application, so a change in B or D does not affect the application (for the current point of view). removing any method or variables from A or C requires so many changes in the application. the whole right score is 2-2, but adding a new method to C is either different. if I added a new method in C, they don’t affect the application, but when adding to A, at least I have to override this method in B and all classes that implement the A interface, since it weakens the connection at least in this scenario . my doubt is, inheritance always gives a tight connection. I learned that Inheritance is one of the powerful OOP tools. For design, if a class follows “relationships,” then use inheritance. loose communication means less information about each other. A and C both in the future do not know which class will implement or extend, but after adding B and D, now B does not depend on A, because all its methods are abstract, but D can also override the inheritance function.

+6
source share
4 answers

Inheritance does not always give a tight connection - because the class you inherit provides a certain way to do this, through which it declares it as private, protected, and open.

A good example of this is the abundance of abstract classes provided by various APIs that implement some of the boiler interface panel features for you and allow you to focus on your own requirements.

Key examples include swing adapter classes that provide no-op implementations of all methods in an interface. More advanced examples actually provide standard implementations of some interface requirements.

Exactly what is a tight connection is actually a very important challenge, in which many things are obviously closely connected with each other, while others are clearly connected with each other, and then there is a large gray area between them.

+2
source

since we know that free communication can reach through the implementation of the interface, and inheritance makes a tight couple.

I think you are wrong. “Binding” usually consists of about two different classes that know each other either by their specific class, or simply by some interface.

Say 2 classes A and B must communicate with each other.

A <--knows--> B 

Methods in will have some parameter B, and methods from B have a parameter of type A. For example. as

  class A { public void talkTo(B b) {} } 

Now that there is a tight connection between A and B, because every change you make to these classes can make changes to another class.

If you do it weakly, they both expose themselves through some kind of interface. (An “interface” may also mean an abstract class — this is the choice of the appropriate side.)

  IA <-- A ^ | \ / X < loose coupling between the A side and the B side / \ v | IB <-- B < pretty tight coupling betwen IB and B 

and the connection between them goes through these interfaces

  class A implements IA { public void talkTo(IB b); } class B implements IB { public void talkTo(IA a); } 

The relationship between A and IA (what you seem to be looking at) is not something that is closely related to loose communication. There are some similarities, but a loose connection does not mean that you must implement an interface and extend an abstract class. It is usually better to implement only the interface.

If you can replace the "IS A" relationship with the "HAS A" relationship, you are essentially doing the same thing. You separate yourself (for example, from you) from a specific implementation and only have to depend on the encapsulated other side (for example, from B). Inheritance is indeed a very powerful feature, but is often used incorrectly.

+16
source

Brief introduction Free and tight communication

Loose Coupling means reducing the dependencies of a class that directly uses another class. With a tight connection, classes and objects depend on each other. In general, tight communication is usually bad because it reduces the flexibility and reuse of the code, and this makes the changes much more complicated and interferes with testing, etc.

Tight connection

A Tightly Coupled Object - an object that needs to know a little about other objects and, as a rule, is very dependent on each other. Changing one object in a closely related application often requires changes for a number of other objects. In a small application, we can easily identify changes and are less likely to miss something. But in large applications, these interdependencies are not always known to every programmer or it is possible to ignore the changes. But each set of loosely coupled objects is independent of each other.

+3
source

there are three problems or levels ... an implementation problem, an object creation problem, and a use problem. When we program the interface not for implementation, we can achieve free communication ... means that any change in the implementation level will have the least effect on the object creation layer ............

0
source

All Articles