Tight Coupling- Suppose we have 2 classes A and B and other class C, D, E are tightly coupled with A and B. In this if we change some code in A, B then dependent classes needs to be changed and so on. So that’s why in real time we have to avoid or try to minimize tight coupling.
Loose Coupling: – In here when we change the change should be isolated we don’t want to change dependent classes of application.
How to achieve loosecoupling?
For that, you need tounderstand isolation by using Encapsulation. Therelationship between classes and most important Interface. It is an Is-a relationship
Ex. In Mobile service center front desk people not supposed to tell how to repair the mobile phone. This is the responsibility of that repairer.
In C# there are 2 types of relationship
- Inheritance – more of tight coupling
- Composition – Composition relationship results in loose coupling.
- Inheritance:-Depict parent-child relationship
- is used for the codeto reuse.
- Polymorphic behavior
- Composition:- Is a kind of relationship between2 classes that allows one object to containinto another object. Is Has-arelationship.
Ex. The bikehas an Engine