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 →
- What are the SOLID principles? Explain each in simple words.
SOLID is five design guidelines. Single responsibility (one reason to change), Open/closed (add features without editing working code), Liskov substitution (a child must work wherever its parent works), Interface segregation (small, focused interfaces) and Dependency inversion (depend on abstractions, not concrete classes).
IntermediateIn-depth3 min read - Does the Open/Closed Principle mean you can never edit an existing class?
No. You still fix bugs and change requirements. OCP is about designing so that common kinds of change, such as adding a new variant, need new code instead of edits to old code.
IntermediateQuick answer1 min read - How do you spot a Single Responsibility violation in real code?
Look for a class whose description needs the word and, imports from unrelated areas, unrelated groups of fields, or edits from different people for different reasons.
IntermediateQuick answer1 min read - How is the Interface Segregation Principle different from the Single Responsibility Principle?
SRP is about a class having one reason to change. ISP is about the interface a client sees: it should be small and fit that client's needs. One is about implementation, the other about the contract.
IntermediateQuick answer1 min read - Show the Open/Closed Principle in JavaScript with a list of validation rules.
Keep rules as a list of small functions and run them all with one loop. A new rule is added by appending a function, and the validating code is never changed.
IntermediateQuick answer1 min read - What are the signs that code violates the Liskov Substitution Principle?
Subclasses that throw UnsupportedOperationException, override a method to do nothing, or callers that must check instanceof before using a parent-typed object.
IntermediateQuick answer1 min read - What is the difference between Dependency Inversion, Dependency Injection and Inversion of Control?
DIP is a design rule: depend on abstractions. DI is a technique: pass dependencies in from outside. IoC is the wider idea that a framework or caller controls the flow instead of your code.
IntermediateQuick answer1 min read - Why does Square extending Rectangle violate the Liskov Substitution Principle?
A Square must keep width and height equal, so setting one changes the other. Code written for any Rectangle, expecting independent sides, gets wrong results when given a Square.
IntermediateQuick answer1 min read