Understanding Dependency Injection and its Importance, A tutorial

Any application is composed with many classes that collaborate each-other to perform some useful stuff. Traditionally each object is responsible for obtaining its own references to the dependent objects (dependencies) it collaborate with. This leads to highly coupled classes and hard-to-test code.
For example ,
Consider a Car object. A Car depends on Wheels, Engine, Fuel, Battery... etc to run. Traditionally we define the brand of such dependent objects along with the definition of Car object.
class Car{
private Wheel wh= new NepaliRubberWheel();
private Battery bt= new ExcideBattery();
Here, Car object is responsible for creating the dependent objects.
What if we want to change the type of its dependent object - say Wheel after the previous NepaliRubberWheel() punctures.
We need to redefine the Car object with its new dependency say ChineseRubberWheel(), But only the Car manufacturer can do that.

Then what the Dependency Injection stand for ...
When using Dependency Injection, objects are given their dependencies on run time rather than compile time(car manufacturing time). In other words objects are configured by an external entity(by us not by manufacturer).
So we can now change the Wheel whenever we want. Here, the Dependency (Wheel) is injected to Car at run time. That is called Dependency Injection.

Benefits of DI

  • Key benefit is loose coupling between dependent objects.If an object operates on their dependencies by their interface not by implementation then compile time dependency can be swapped out with Dependency Injection.
  • Very useful when we have objects (Wheel) whose implementations change often ( if the type punctures often we can change it easily) ?
  • Very useful for large projects where there is issue of maintainability, simplicity and many others ty…


  1. Good presentation, but I am not sure I want loose coupling of wheels to my car... Not on the road, at least! ;-)

  2. Hi could you show how the code would look like after DI? as in the instantiation would come in the constructor is it?

    1. Please take a look at this :


Your Comment and Question will help to make this blog better...