Inversion of control and Dependency Injection

Heard about this when I started my career as a software engineer. Never understood it, even when I did, it wouldn’t stick for long in my head until I used it extensively in the spring framework.

Inversion of control is a Design principle which lets you achieve decoupling of modules/responsibilities while Dependency injection is a design pattern about providing dependencies to a Class or Method.

The core idea behind Inversion of Control is to give control to a different class to create the instances of classes you need in your application.

If you are an Android Developer you would have noticed that the onCreate method of your Activity class provides the already created Bundle object. Since Bundle instance was created somewhere else and is passed into the onCreate method its called Dependency Injection, we are passing the dependency to a method. And the principle behind this is called Inversion of control.

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    protected void onCreate(Bundle savedInstanceState) {


Another Example

Without Inversion of Control

public class TextEditor {

    private SpellChecker checker;

    public TextEditor() {
        this.checker = new SpellChecker();

Spell checker is created inside the TextEditor class constructor.

With Inversion of Control

public class TextEditor {

    private IocSpellChecker checker;

    public TextEditor(IocSpellChecker checker) {
        this.checker = checker;

The instance of SpellChecker is created by someone else most probably by a framework. Spring framework is completely built using the principle of Inversion of control. The framework handles the creation of objects (singleton or not) and injects them into classes where they are needed using tags like @Autowired, @Controller etc.