Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- How do you do dependency injection in JavaScript without any framework?
Pass the dependency as an argument, either to a factory function or a class constructor. A function that receives what it needs is already using dependency injection.
BeginnerQuick answer1 min read - How does dependency injection make unit testing easier?
Because the class receives its collaborators, a test can pass in a fake instead of a real database, network or clock. The test runs fast and gives the same result every time.
BeginnerQuick answer1 min read - What are constructor injection and setter injection, and which should you prefer?
Constructor injection passes dependencies when the object is created, so it is never half-built. Setter injection sets them afterwards. Prefer the constructor for required dependencies and setters only for optional ones.
BeginnerQuick answer1 min read - Why is creating dependencies with new inside a class considered a problem?
It hard-wires the class to one concrete implementation, hides what it depends on, and makes it impossible to replace in tests or in other environments without editing the class.
BeginnerQuick answer1 min read