๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Dependency (USES-A Relationship) in Java  


Dependency (USES-A relationship)
  • Definition :
    • Dependency is a relationship where one class uses another class temporarily to perform a specific task. This means the dependent object is often used within a method, rather than being stored as an instance variable.
    • It represents a USES-A relationship.
    • The dependency is typically short-lived, existing only during the execution of a method.
  • Example :
    • Teacher USES-A Whiteboard
    • Office Worker USES-A Printer
  • How to achieve dependency ?
    • Dependency is achieved by creating or using objects of another class inside a method, instead of holding them as instance variables.
    • We can inject dependent objects using:
      1. Local Variable inside Method
        • The dependent object is created directly in a method
      2. Method Parameter
        • The dependent object is passed as a method argument
        • It promotes more flexibility and testability
  • Program 1 (Dependency using Local Variable inside Method) :
    // Dependent class
    class Whiteboard
    {
        void writeOnBoard()
        {
            System.out.println("Writing on the whiteboard...");
        }
    }
    
    // Main class that uses Whiteboard
    class Teacher
    {
        void teachLesson()
        {
            // Local variable: Dependency created inside the method
            Whiteboard board = new Whiteboard();
            board.writeOnBoard();  // Temporary usage
            System.out.println("Teacher is explaining the topic.");
        }
    }
    
    // Entry point
    public class MainApp
    {
        public static void main(String[] args)
        {
            Teacher teacher = new Teacher();
            teacher.teachLesson();  // Trigger method that shows dependency
        }
    }
    Output:
    Writing on the whiteboard...
    Teacher is explaining the topic.
  • Program 2 (Dependency using Method Parameter) :
    // Dependent class
    class Printer
    {
        void printDocument()
        {
            System.out.println("Printing document...");
        }
    }
    
    // Main class that depends on Printer
    class OfficeWorker
    {
        // Dependency injected via method parameter
        void performTask(Printer printer)
        {
            printer.printDocument();  // Temporary usage
            System.out.println("OfficeWorker has completed printing task.");
        }
    }
    
    // Entry point
    public class MainApp
    {
        public static void main(String[] args)
        {
            Printer printer = new Printer();            // Create dependency
            OfficeWorker worker = new OfficeWorker();   // Create dependent
    
            // Inject dependency via method parameter
            worker.performTask(printer);
        }
    }
    Output:
    Printing document...
    OfficeWorker has completed printing task.

Dependency vs Association:
Feature Association (HAS-A) Dependency (USES-A)
Duration Long-term (object is stored) Temporary (used in a method)
Object location Instance variable Local variable or parameter
Example Car has an Engine OfficeWorker uses a Printer
Usage Reused across methods Used only within one method