Coupling in Java with Example

Introduction

Coupling refers to the degree of direct knowledge that one class has about another class. In software design, coupling is an essential concept that impacts the modularity and maintainability of a system. Lower coupling between classes usually leads to a system that is easier to maintain and extend, while higher coupling can make the system more rigid and harder to manage.

Table of Contents

  1. What is Coupling?
  2. Types of Coupling
  3. Benefits of Low Coupling
  4. Example: Tight Coupling
  5. Example: Loose Coupling
  6. Conclusion

1. What is Coupling?

Coupling describes the degree of dependency between classes or modules. It indicates how closely connected different classes or modules are, and how much they rely on each other. There are two main types of coupling:

  • Tight Coupling: High dependency between classes. Changes in one class often require changes in the coupled class.
  • Loose Coupling: Low dependency between classes. Classes interact through well-defined interfaces, reducing the impact of changes.

2. Types of Coupling

Tight Coupling

Tight coupling occurs when a class is highly dependent on the specifics of another class. This often happens when one class creates an instance of another class and directly accesses its methods and fields.

Loose Coupling

Loose coupling occurs when classes are less dependent on the specifics of other classes. This can be achieved by using interfaces, abstract classes, or dependency injection, allowing classes to interact without knowing the implementation details of each other.

3. Benefits of Low Coupling

  • Improved Maintainability: Changes in one class have minimal impact on other classes.
  • Enhanced Reusability: Loosely coupled classes can be reused in different contexts.
  • Better Testability: Independent classes are easier to test in isolation.
  • Flexibility: Systems with low coupling are more adaptable to change.

4. Example: Tight Coupling

In a tightly coupled system, changes in one class can significantly affect other classes.

Example:

class Engine {
    public void start() {
        System.out.println("Engine started.");
    }
}

class Car {
    private Engine engine;

    public Car() {
        this.engine = new Engine(); // Direct instantiation
    }

    public void start() {
        engine.start();
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.start(); // Output: Engine started.
    }
}

Explanation:

  • Car class is tightly coupled with the Engine class.
  • The Car class directly creates an instance of Engine and calls its start method.
  • Any change in the Engine class, such as a change in the method signature, would require changes in the Car class.

5. Example: Loose Coupling

In a loosely coupled system, classes interact through interfaces, reducing dependency.

Example:

interface Engine {
    void start();
}

class DieselEngine implements Engine {
    @Override
    public void start() {
        System.out.println("Diesel Engine started.");
    }
}

class ElectricEngine implements Engine {
    @Override
    public void start() {
        System.out.println("Electric Engine started.");
    }
}

class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }

    public void start() {
        engine.start();
    }
}

public class Main {
    public static void main(String[] args) {
        Engine dieselEngine = new DieselEngine();
        Car carWithDieselEngine = new Car(dieselEngine);
        carWithDieselEngine.start(); // Output: Diesel Engine started.

        Engine electricEngine = new ElectricEngine();
        Car carWithElectricEngine = new Car(electricEngine);
        carWithElectricEngine.start(); // Output: Electric Engine started.
    }
}

Explanation:

  • Engine is an interface that defines the start method.
  • DieselEngine and ElectricEngine are implementations of the Engine interface.
  • Car class depends on the Engine interface rather than a concrete implementation.
  • The Car class can work with any implementation of the Engine interface, promoting loose coupling.

6. Conclusion

Coupling is a critical concept in software design that affects the modularity, maintainability, and flexibility of a system. Tight coupling leads to a system where classes are highly dependent on each other, making it hard to maintain and extend. Loose coupling, on the other hand, reduces dependencies, making the system easier to manage and adapt to changes. By using interfaces, abstract classes, and dependency injection, developers can achieve loose coupling and build more robust and flexible systems.

Happy coding!

Comments

  1. Hi where have you used getv() method in your example ? if not than why have you used it . what is the purpose of the above method ?

    ReplyDelete
    Replies
    1. getV() is a getter method for Vehicle object. We create getter and setter methods to get and set values to variable or object right.

      Delete

Post a Comment

Leave Comment