OOP · OOP Relationships
Dependency
Provides strong encapsulation.
Advantages
- Provides strong encapsulation.
- Ensures data consistency.
- Simplifies lifecycle management.
- Models real-world part-of relationships.
Real-World Problem
Imagine you are developing an Online Shopping Application. When a customer searches for a product, the ProductService needs product information. Instead of storing the data itself, the service uses the ProductRepository to fetch the product details. Once the task is completed, the interaction ends.
This temporary relationship is called Dependency. What is Dependency?
Dependency is an OOP relationship where one class temporarily uses another class to perform a specific task. The dependent class does not own or manage the lifecycle of the object it uses. It simply relies on it whenever required. Dependency represents a Uses-A relationship.
Diagram Explanation The diagram shows how Dependency works.
- The User sends a request to the ProductService.

- The ProductService temporarily uses the ProductRepository to fetch product details.
- The Repository retrieves the required data and returns it to the Service.
- The Service sends the result back to the user.
- The ProductService does not own the ProductRepository; it only depends on it to complete the request.
Key Point: Dependency is a Uses-A relationship where one class temporarily relies on another class to perform a task.
Key Characteristics
- Uses-A relationship.
- Temporary dependency.
- No ownership.

The weakest link: one class temporarily uses another, usually via a method parameter.
class Report {
String content = "Quarterly numbers";
}
class Printer {
// Printer depends on Report only for the duration of this call
void print(Report report) {
System.out.println(report.content);
}
}
// Printer holds no Report field — it just needs one to do its job.