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 →
- Does the Single Responsibility Principle mean a class should have only one method?
No. It means a class should have one reason to change, serving one actor or concern. A class can have many methods if they all serve that single purpose.
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