0% found this document useful (0 votes)
70 views9 pages

What Is Coupling and What Do You Understand by Tight and Loose Coupling?

Tight coupling occurs when classes are highly dependent on each other. Loose coupling is achieved through interfaces and abstraction, allowing classes to be independently developed and tested. Method overloading demonstrates static/compile-time polymorphism through static binding, while method overriding shows dynamic/runtime polymorphism via dynamic binding.

Uploaded by

Neil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views9 pages

What Is Coupling and What Do You Understand by Tight and Loose Coupling?

Tight coupling occurs when classes are highly dependent on each other. Loose coupling is achieved through interfaces and abstraction, allowing classes to be independently developed and tested. Method overloading demonstrates static/compile-time polymorphism through static binding, while method overriding shows dynamic/runtime polymorphism via dynamic binding.

Uploaded by

Neil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

What is coupling and what do you understand by tight and loose

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.

Loose coupling is achieved by means of a design that promotes single-responsibility and


separation of concerns. A loosely-coupled class can be consumed and tested independently of
other (concrete) classes.

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.

Example of tight coupling:

class CustomerRepository
{
private readonly Database database;

public CustomerRepository(Database database)


{
this.database = database;
}

public void Add(string CustomerName)


{
database.AddRow("Customer", CustomerName);
}
}

class Database
{
public void AddRow(string Table, string Value)
{
}
}

Example of loose coupling:

class CustomerRepository
{
private readonly IDatabase database;

public CustomerRepository(IDatabase database)


{
this.database = database;
}

public void Add(string CustomerName)


{
database.AddRow("Customer", CustomerName);
}
}

interface IDatabase
{
void AddRow(string Table, string Value);
}

class Database : IDatabase


{
public 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.

Tight coupling (Detailed Example)


Think of your skin. It's stuck to your body. It fits like a glove. But what if you wanted to change
your skin colour from say white to black? Can you imagine just how painful it would be to peel
off your skin, dye it, and then to paste it back on etc? Changing your skin is difficult because it is
tightly coupled to your body. You just can't make changes easily. You would have to
fundamentally redesign a human being in order to make this possible.
 Key Point #1: In other words, if you want to change the skin, you would also HAVE TO
change the design of your body as well because the two are joined together - they are
tightly coupled.

God was not a good object oriented programmer.

Loose coupling (Detailed Example)


Now think of getting dressed in the morning. You don't like blue? No problems: you can put a
red shirt on instead. You can do this easily and effortlessly because the shirt is not really
connected to your body the same way as your skin. The shirt doesn't know or care about what
body it is going on. In other words, you can change your clothes, without really changing your
body.

 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.

In tight coupling, there are hard-coded dependencies declared in methods.


In loose coupling, we must pass dependency externally at runtime instead of hard-coded. (Loose
couple systems use interface for decreased dependency with class.)

For example, we have a system that can send output in two or more ways like JSON output, CSV
output, etc.

Tight Coupled Example

public interface OutputGenerator {


public void generateOutput();
}
public class CSVOutputGenerator implements OutputGenerator {
public void generateOutput() {
System.out.println("CSV Output Generator");
}
}
public class JSONOutputGenerator implements OutputGenerator {
public void generateOutput() {
System.out.println("JSON Output Generator");
}
}

// In Other Code, we write Output Generator like...


public class Class1 {
public void generateOutput() {
// Here Output will be in CSV-Format, because of hard-coded code.
// This method tightly coupled with CSVOutputGenerator class, if we
want another Output, we must change this method.
// Any method, that calls Class1's generateOutput will return
CSVOutput, because Class1 is tight couple with CSVOutputGenerator.
OutputGenerator outputGenerator = new CSVOutputGenerator();
output.generateOutput();
}
}

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.

Loose Coupled Example


public interface OutputGenerator {
public void generateOutput();
}

public class CSVOutputGenerator implements OutputGenerator {


public void generateOutput() {
System.out.println("CSV Output Generator");
}
}

public class JSONOutputGenerator implements OutputGenerator {


public void generateOutput() {
System.out.println("JSON Output Generator");
}
}

// In Other Code, we write Output Generator like...


public class Class1 {
public void generateOutput(OutputGenerator outputGenerator) {
// if you want to write JSON, pass object of JSONOutputGenerator
(Dependency will be passed externally to this method)
// if you want to write CSV, pass object of CSVOutputGenerator
(Dependency will be passed externally to this method)

// Due to loose couple with class, we don't need to change code of


Class1, because Class1 is loose coupled with CSVOutputGenerator or
JSONOutputGenerator class
// Any method, that calls Class1's generateOutput will desired
output, because Class1 does not tight couple with CSVOutputGenerator or
JSONOutputGenerator class
OutputGenerator outputGenerator = outputGenerator;
output.generateOutput();
}
}

----------------------------------------------------------------------------------------------------------------
http://javaconceptoftheday.com/static-binding-and-dynamic-binding-in-java/
Polymorphism:
1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)

2. Dynamic binding/Run-Time binding/Late binding/Method overriding.(in different


classes)

Compile time Polymorphism (Static Binding/Early Binding): In static polymorphism, if we


call a method in our code then which definition of that method is to be called actually is resolved
at compile time only.
(Or)
At compile time, Java knows which method to invoke by checking the method signatures. So,
this is called compile-time polymorphism or static binding.

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.

Static polymorphism (Compile-time Polymorphism)

 Static Polymorphism decides which method to execute during compile time.


 Method Overloading is an example of static polymorphism, and it is required to happens
static polymorphism.
 Static Polymorphism achieved through static binding.
 Static Polymorphism happens in the same class.
 Object assignment is not required for static polymorphism.
 Inheritance not involved for static polymorphism.

Dynamic Polymorphism (Runtime Polymorphism)

 Dynamic Polymorphism decides which method to execute in runtime.


 Method Overriding is an example of dynamic polymorphism, and it is required to
happens dynamic polymorphism.
 Dynamic Polymorphism achieved through dynamic binding.
 Dynamic Polymorphism happens between different classes.
 It is required where a subclass object is assigned to super class object for dynamic
polymorphism.
 Inheritance involved for dynamic polymorphism.
Method Overloading is known as Static Polymorphism and also Known as Compile Time
Polymorphism or Static Binding because overloaded method calls get resolved at compile time
by the compiler on the basis of the argument list and the reference on which we are calling the
method.

Method Overriding is known as Dynamic Polymorphism or simple Polymorphism or


Runtime Method Dispatch or Dynamic Binding because overridden method call get resolved
at runtime.

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);}

public static void main(String args[]) {


Calculation obj=new Calculation();
obj.sum(10,10,10); // 30
obj.sum(20,20); //40
}
}

Overriding example:
class Animal {
public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal {

public void move() {


System.out.println("Dogs can walk and run");
}
}

public class TestDog {

public static void main(String args[]) {


Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move();//output: Animals can move

b.move();//output:Dogs can walk and run


}
}
 Method overloading would be an example of static polymorphism
 Method overriding would be an example of dynamic polymorphism.

Static Binding in Java:


Static binding is a binding which happens during compilation. It is also called early binding
because binding happens before a program actually runs.

Static binding can be demonstrated like in the below picture.

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.

Dynamic Binding in Java:


Dynamic binding is a binding which happens during run time. It is also called late binding
because binding happens when program actually is running.

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.

The dynamic binding of above example can be demonstrated like below.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy