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 - A class calculates salary, saves it to the database and prints a payslip. How does it break SRP, and how do you fix it?
It has three reasons to change: pay rules, database layout and payslip format. Split it into three classes, each with one job, and let a small coordinator call them.
BeginnerQuick answer1 min read - 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 - Explain the Interface Segregation Principle with a Worker interface example.
No class should be forced to implement methods it does not use. Split one big interface into small, focused ones so each class picks only what applies.
BeginnerQuick answer1 min read - Explain the Open/Closed Principle with a payment example.
Classes should be open for extension but closed for modification. To add a new payment method, write a new class that implements an interface instead of editing the checkout code.
BeginnerQuick answer1 min read - Show Dependency Inversion with a NotificationService that currently creates EmailSender inside itself.
Define a Sender interface, have NotificationService depend on it, and pass the implementation in from outside. The service no longer knows whether it is sending email, SMS or WhatsApp.
BeginnerQuick answer1 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 - In the Dependency Inversion Principle, who should own the interface: the high-level or the low-level module?
The high-level module. It declares the interface in terms of what it needs, and low-level modules implement it. That is what inverts the dependency direction.
AdvancedQuick answer1 min read - What rules must an overriding method follow to respect the Liskov Substitution Principle?
It must accept at least what the parent accepts, promise at least what the parent promises, and throw no new kinds of failure. Weaker preconditions, stronger postconditions, same invariants.
AdvancedQuick answer1 min read - Your interface has twelve methods and most implementers leave several empty. How do you fix it without breaking everything?
Introduce smaller role interfaces, make the big interface extend them, move clients to the small ones gradually, then retire the old one. Existing code keeps compiling throughout.
AdvancedQuick answer1 min read