Object-Oriented Programming (OOP) concepts are a staple of technical interviews across all company types in India: from TCS and Infosys fresher rounds to SDE-2 interviews at Flipkart and Razorpay. Many candidates memorise the four pillars but struggle to apply them in design questions. This guide covers the theory with interview-ready answers and the practical application that separates strong candidates from average ones.
The four pillars with interview-ready answers
Encapsulation: Bundling data (attributes) and the methods that operate on that data within a class, and restricting direct access to the data from outside the class. Achieved in Java/Python through private/protected access modifiers and getter/setter methods. Why it matters: prevents external code from putting an object into an invalid state.
Inheritance: A class (child/subclass) inherits attributes and methods from another class (parent/superclass), allowing code reuse and establishing an 'is-a' relationship. Example: Dog extends Animal: a Dog 'is-an' Animal. Key interview point: prefer composition over inheritance when the relationship is 'has-a' not 'is-a' (a Car 'has-an' Engine, not 'is-an' Engine).
Polymorphism: An object can take many forms. Two types: compile-time polymorphism (method overloading: same method name, different parameters) and runtime polymorphism (method overriding: subclass provides its own implementation of a parent method). The latter is the more powerful form: it enables code that works on a base type to work differently depending on the actual subtype at runtime.
Abstraction: Hiding implementation details and exposing only what is necessary. Achieved through abstract classes (can have some implementation) and interfaces (no implementation in Java 7 and earlier, default methods allowed since Java 8). Abstraction allows you to change implementation without changing the interface.
SOLID principles with examples
SOLID is tested at SDE-2+ interviews at Indian product companies:
S: Single Responsibility Principle: A class should have only one reason to change. Bad example: a User class that handles user data, sends emails, and writes to the database. Good example: separate UserService (business logic), UserRepository (database), and EmailService.
O: Open/Closed Principle: Open for extension, closed for modification. Add new behaviour by extending existing classes, not modifying them. Example: instead of adding if/else to a payment processor for each payment type, define a PaymentStrategy interface and add new payment types as new implementations.
L: Liskov Substitution Principle: A subclass must be substitutable for its parent class without changing correctness. Classic violation: Square extends Rectangle and overrides setWidth to also set height: violates LSP because code that works with Rectangle breaks with Square.
I: Interface Segregation Principle: Do not force clients to depend on interfaces they do not use. Split large interfaces into smaller, focused ones. Example: instead of one large Animal interface with fly(), swim(), and run(), create separate Flyable, Swimmable, Runnable interfaces.
D: Dependency Inversion Principle: Depend on abstractions, not concretions. High-level modules should not depend on low-level modules: both should depend on abstractions. Achieved through dependency injection. Example: OrderService should depend on PaymentGateway (interface), not StripePaymentGateway (concrete class).
Design patterns most asked in India
Design patterns are tested at SDE-2 level and above:
Singleton: Ensures only one instance of a class exists. Common use: database connection pool, configuration manager. Thread-safety is a critical interview follow-up: use double-checked locking or enum-based singleton in Java.
Factory Method: A method that creates objects without specifying the exact class to create. Used when the type of object to create depends on runtime information. Example: DatabaseFactory.create(config) returns MySQLDatabase or PostgreSQLDatabase based on config.
Observer: Defines a one-to-many dependency: when one object changes state, all dependents are notified automatically. Used everywhere: UI event handling, notification systems, Kafka consumer-producer model.
Strategy: Defines a family of algorithms, encapsulates each, and makes them interchangeable. Example: sorting strategy (quicksort vs mergesort), payment strategy (UPI vs card vs netbanking). Avoids if/else chains for algorithm selection.
Decorator: Attaches additional responsibilities to an object dynamically. Used to extend object behaviour without subclassing. Example: Java I/O streams (BufferedInputStream wraps FileInputStream).
Repository Pattern: Abstracts data access logic. Service layer talks to a Repository interface, not directly to the database. Enables easy swapping of data sources and testability via mock repositories.
Frequently asked questions
Practice these questions on HireStepX