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 →
- 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