Coupling in Java with Example


< Previous Next >

OOPS Tutorial


1. Overview

In this article, we will learn the important object-oriented concept Coupling. Coupling refers to the degree to which one class knows about another class. If one class uses another class, that is coupling.

2. Intent/Definition

Coupling refers to the degree to which one class knows about another class. If one class uses another class, that is coupling. Low dependencies between “artifacts” (classes, modules, components).There shouldn’t be too much of dependency between the modules, even if there is a dependency it should be via the interfaces and should be minimal.

Key Points

  • While creating a complex application in java, the logic of one class will call the logic of another class to provide the same service to the clients.
  • If one class calling another class logic then it is called collaboration.
  • When one class is collaborating with another class then there exists a tight-coupling between the two classes.
  • If one class wants to call the logic of a second class then they first class need an object of second class it means the first class creates an object of the second class.

3. Two types of Coupling

  1. Tight-coupling
  2. Loose-coupling

3.1 Tight Coupling

If one class tightly coupled with other class then it is tight-coupling.
Implementation
This is an example of tight coupling. Here to start the journey, the Traveler class is creating Car object to interact with it using move() method.
In the above example the traveler object is tightly coupled with car object because in place car object if you want to use bike object then, we need to make changes in Traveller class. Hence tight coupling should be avoided.
Step 1: Create a Car class.
public class Car  {
    @Override
    public void move() {
         System.out.println("Car is moving");
    }
}
Step 2: Create Traveler class which hold the reference of Car class. Traveler class is tightly coupled with Car class.
class Traveler {
    Car c = new Car();
    public void startJourney() {
         c.move();
    }
}

3.2 Loose Coupling

  1. Low dependencies between “artifacts” (classes, modules, components).
  2. There shouldn’t be too much of dependency between the modules, even if there is a dependency it should be via the interfaces and should be minimal.
  3. Avoid tight-coupling for collaboration between two classes (if one class wants to call the logic of a second class, then the first class needs an object of second class it means the first class creates an object of the second class).
  4. Strive for loosely coupled design between objects that interact.
  5. Inversion Of Control (IoC) / Dependency Injection (DI) - With DI objects are given their dependencies at creation time by some third party (i.e. Java EE CDI, Spring DI…) that coordinates each object in the system. Objects aren’t expected to create or obtain their dependencies—dependencies are injected into the objects that need them. The key benefit of DI—loose coupling.
Implementation
This is an example of loose coupling. In this class, Traveler class is not tightly coupled with Car or Bike implementation. Instead by applying dependency injection mechanism, the loose coupling implementation is achieved to allow start journey with any class which has implemented Vehicle interface.
Step 1: Vehicle interface to allow loose coupling implementation.
interface Vehicle {
      public void move();
}
Step 2: Car class implements the Vehicle interface.
class Car implements Vehicle {
    @Override
    public void move() {
         System.out.println("Car is moving");
    }
}
Step 3: Bike class implements Vehicle interface.
class Bike implements Vehicle {
    @Override
    public void move() {
         System.out.println("Bike is moving");
    }
}
Step 4: Now create Traveler class which holds the reference to Vehicle interface.
class Traveler {
    private Vehicle v;
    public Vehicle getV() {
          return v;
    }
    public void setV(Vehicle v) {
         this.v = v;
    }

    public void startJourney() {
         v.move();
    }
}
Step 5: Test class for loose coupling example - Traveler is an example of loose coupling.
public static void main(String[] args) {
    Traveler traveler = new Traveler();
    traveler.setV(new Car()); // Inject Car dependency
    traveler.startJourney(); // start journey by Car
    traveler.setV(new Bike()); // Inject Bike dependency
    traveler.startJourney(); // Start journey by Bike
}

Related Oops Posts

Top Core Java Tutorials

  1. Java Tutorial for Beginners
  2. 50 Java Keywords
  3. JDBC 4.2 Tutorial
  4. All Java/J2EE Tutorial
  5. Java 8 Tutorial
  6. Java Collections Tutorial
  7. Java Exceptions Tutorial
  8. Java Generics Tutorial
  9. Java 8 Stream API Tutorial
  10. Java Wrapper Classes
  11. Java Arrays Guide
  12. Java Multithreading Tutorial
  13. Java Concurrency Tutorial
  14. Oops Concepts Tutorial
  15. Java String API Guide
  16. Java Reflection API Tutorial
  17. Java I/O Tutorial
  18. Date and Time API Tutorial
  19. JUnit 5 Tutorial
  20. JUnit 4 Tutorial
  21. Java XML Tutorial
  22. Google GSON Tutorial

Top Java EE Tutorials

  1. Spring Security Tutorial
  2. RabbitMQ Tutorial
  3. Hibernate ORM 5
  4. Java Persistence API
  5. Spring Boot 2 Tutorial
  6. Spring Core 5 Tutorial
  7. Spring MVC 5 Tutorial
  8. Spring Data JPA Tutorial
  9. Apache HttpClient Tutorial
  10. Spring Framework 5
  11. Apache Maven Tutorial
  12. JAX-RS Tutorial
  13. Jersey Rest Tutorial

Java Library Tutorials

  1. Java API Guides
  2. Java SQL Package Tutorial
  3. All Java/J2EE Tutorial
  4. Java Lang Package Tutorial
  5. Java Util Package Tutorial
  6. Java Lang Reflect Package Tutorial
  7. Java Time Package Tutorial
  8. Java IO Package Tutorial

Top Java Best Practices

  1. Java Enums and Annotations Best Practices
  2. Java Generics Best Practices
  3. JUnit Framework Best Practices
  4. Java Naming Conventions
  5. Single Responsibility Principle
  6. Liskov's Substitution Principle
  7. Interface Segregation Principle
  8. Dependency Inversion Principle
  9. Open Closed Principle
  10. Oops principles in java
  11. Restful API Best Practices
  12. JSP Best Practices
  13. Guide to JDBC Best Practices
  14. Collection Best Practices
  15. String Best Practices in Java
  16. Exception Handling Best Practices
  17. Synchronization Best Practices
  18. Guide to JDBC Best Practices
  19. Serialization Best Practices

 
The source code of this post is available on GitHub : Object-Oriented Design Guide

Comments