What is coupling in OOP
Loading
What is coupling in OOP
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajanikant HawaldarPosted Sep 25, 2021, 6:43 AM
Brahma Prakash ShuklaPosted Oct 29, 2022, 2:47 PM
https://www.geeksforgeeks.org/coupling-in-java/#:~:text=In%20object%20oriented%20design%2C%20Coupling,two%20classes%20often%20change%20together.
Naresh BeniwalPosted Oct 28, 2022, 5:07 PM
https://www.geeksforgeeks.org/coupling-in-java/#:~:text=In%20object%20oriented%20design%2C%20Coupling,two%20classes%20often%20change%20together.
Mukesh NailwalPosted Nov 2, 2021, 5:21 AM
satheesh dPosted Nov 1, 2021, 12:58 PM
Coupling is a degree of dependency of one class on another class.And there are two types of coupling are available(Loose Coupling and Tight Coupling).
In the tight coupling, one class is heavily coupled on another class. Two classes are often change together. That means, If you make changes in one class, then changes are also made on another class which coupled to it.In Loose coupling ther is less dependency between the classes.
public class Bus
{
public void start()
{
Console.WriteLine("Bus----");
}
}
public class Car
{
public void start()
{
Console.WriteLine("Car----");
}
}
public class Travel
{
public void start()
{
Bus bus = new Bus();
bus.start();
}
}
public class traveller
{
public static void main(String[] args)
{
Travel travel = new Travel();
travel.start();
}
}
In this Example, the traveller class is tightle coupled with travel class. If I need to change my travel mode then I need to change in my travel class. Also the bus and car class is also tightly coupled with travel class. If I changed my method in bus class then I also need to change in my travel class.
public Interface Transport
{
void start();
}
public class Bus:Transport
{
public override void start()
{
Console.WriteLine("---bus");
}
}
public class car:Transport
{
public override void start()
{
Console.WriteLine("---car");
}
}
public interface ITravel
{
void start();
}
public class travel
{
private Transport transport;
public override void start()
{
Console.WriteLine("---car");
}
}
public class traveller
{
public static void main(String[] args)
{
Transport transport1 = new Bus();
ITravel travel = new Travel(transport1);
}
}
This is the example for the loosely coupled class. Here the dependency is less between the classes and testing will be easy in these types of applications.
Sachin SinghPosted Sep 24, 2021, 8:05 PM
Farhan AhmedPosted Sep 24, 2021, 4:38 PM
Coupling in C# describes the relationship between modules in C#, how the classes and objects are connected, and also how dependent they are on each other. This also describes the flexibility and reusability part of the code. There are two types of coupling
Tightly coupled: Think of it this way - your fingers are tightly coupled with the hand, which makes it hard to change the position of the fingers and their location. This means that changing the position of fingers is close to impossible and requires redesigning of the hand. This is an example of being tightly coupled.
Loosely coupled: Loosely coupled can be explained with the help of the head as an example. Take any type of hat, you will be able to fit it on your head. For example, a cowboy hat can fit on top of it, so can a chef's hat. Here, the head and the cap are loosely coupled as both are not interdependent. This makes it much less complex and ensures code reusability.