[General boards] [Winter 2019 courses] [Fall 2018 courses] [Summer 2018 courses] [Older or newer terms]

Is this good dependency injection?


#1

isthis

I know that dependency injection is possible using setter or constructor.
This is using setter but obj=new Moogah(); this line makes me confused.


#2

I would say its not because even though they do use dependency injection, they negate it with calling the constructor anyway and then overriding it. The answer that had no constructors I feel was a better answer.


#3

Agreed. The goal of dependency injection is to lessen dependencies on other classes. But here, you could not test ExamDemo separately from Moogah. Also you cannot change the constructor for Moogah or replace it with its own subclass without making changes to Exam Demo. This is not properly implemented dependency injection.


#4

(1) The first assignment to obj1 is useless.
(2) setObj() requires that Moogah has a ctor taking no arguments.