---
# System prepended metadata

title: Dependency Injection

---


# Dependency Injection

**Dependency Injection:** the object gets its required objects from outside (via constructor or methods)
**inversion of control:** rather than the class creating which object it needs to use, the client, Identifies it by passing it via the constructor
**hard dependency:** when a class contains an object instantiate via the new operator
**what is the problem with hard dependency?**
it causes you can not test the class individually.

## **dependency injection benefits**
1.  it makes classes as independent as possible which's leads to an increase in the possibility of reusing these classes (reusability).
2.  when you test the class you can use mock classes to replace needed objects(**mock objects:** objects which behave similar as the real object but they are not real programed).

## DependencyInjection Frameworks
Frameworks that make it easy to instantiate object when you use dependency injection technique
**examples:** (java -> Spring & Guice), (Android -> Dagger).