What Is Coupling and What Do You Understand by Tight and Loose Coupling?
What Is Coupling and What Do You Understand by Tight and Loose Coupling?
coupling?
Tight coupling is when a group of classes are highly dependent on one another.
This scenario arises when a class assumes too many responsibilities, or when one concern is
spread over many classes rather than having its own class.
Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces
rather than other concrete classes, and any class can be on the other end of that communication
simply by implementing the interface.
class CustomerRepository
{
private readonly Database database;
class Database
{
public void AddRow(string Table, string Value)
{
}
}
class CustomerRepository
{
private readonly IDatabase database;
interface IDatabase
{
void AddRow(string Table, string Value);
}
Summary Example:
The Hat is "loosely coupled" to the body. This means you can easily take the hat off without
making any changes to the person/body. When you can do that then you have "loose coupling".
See below for elaboration.
That's key point #2. 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.
---------------------------------------------------------------------------------------------------------------------
Tight Coupling means one class is dependent on another class.
Loose Coupling means one class is dependent on interface rather than class.
For example, we have a system that can send output in two or more ways like JSON output, CSV
output, etc.
In the example above, if we want to change the output in JSON, then we need to find and change
in the whole code, because Class1 is tightly coupled with the CSVOutputGenerator class.
----------------------------------------------------------------------------------------------------------------
http://javaconceptoftheday.com/static-binding-and-dynamic-binding-in-java/
Polymorphism:
1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)
Dynamic Polymorphism (Late Binding/ Runtime Polymorphism): At run time, Java waits
until runtime to determine which object is actually being pointed to by the reference. Method
resolution was taken at runtime, due to that we call as run time polymorphism.
Overloading example:
class Calculation {
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
Overriding example:
class Animal {
public void move(){
System.out.println("Animals can move");
}
}
In this picture, ‘a1’ is a reference variable of type Class A pointing to object of class A. ‘a2’ is
also reference variable of type class A but pointing to object of Class B.
During compilation, while binding, compiler does not check the type of object to which a
particular reference variable is pointing. It just checks the type of reference variable through
which a method is called and checks whether there exist a method definition for it in that type.
For example, for “a1.method()” method call in the above picture, compiler checks whether there
exist method definition for method() in Class A. Because ‘a1′ is Class A type. Similarly, for
“a2.method()” method call, it checks whether there exist method definition for method() in Class
A. Because ‘a2′ is also Class A type. It does not check to which object, ‘a1’ and ‘a2’ are
pointing. This type of binding is called static binding.
During run time actual objects are used for binding. For example, for “a1.method()” call in the
above picture, method() of actual object to which ‘a1’ is pointing will be called. For
“a2.method()” call, method() of actual object to which ‘a2’ is pointing will be called. This type
of binding is called dynamic binding.