Quick answer: These 40 OOPs interview questions and answers cover what is asked most in software engineer, Java, Python and C++ developer interviews: OOP basics, encapsulation and abstraction, inheritance, polymorphism, constructors and special members, and SOLID principles and design patterns. Key answers include short code examples.
Read each answer, then close the page and explain it aloud in two or three sentences. That is exactly what interviewers check.
Contents
- OOP Basics
- Encapsulation and Abstraction
- Inheritance
- Polymorphism
- Constructors and Object Lifecycle
- Design and Practical Questions
- How to prepare
- FAQs
1. OOP Basics Interview Questions
Q1. What is object-oriented programming?
Object-oriented programming organises software around objects that combine data (attributes) and behaviour (methods). It models real-world entities, such as a BankAccount with a balance and deposit and withdraw methods, and makes code easier to reuse and maintain.
Q2. What are the four pillars of OOP?
Encapsulation (bundling data with methods and restricting direct access), abstraction (showing only essential features), inheritance (creating new classes from existing ones) and polymorphism (one interface, many forms).
Q3. What is the difference between procedural and object-oriented programming?
Procedural programming organises code as functions that act on separate data, as in C. OOP groups data and the functions that use it into objects, which improves modularity, reuse and security of data, as in Java, C++ and Python.
Q4. What is a class and what is an object?
A class is a blueprint that defines attributes and methods. An object is an instance of a class with its own values. Car is a class; a red car with number TS09AB1234 is an object.
Q5. Does a class occupy memory?
A class definition itself does not hold object data; memory for attributes is allocated when an object is created. Static members are stored once for the class.
Q6. Which languages support OOP?
Java, C++, C#, Python, Kotlin, Ruby and JavaScript (prototype-based, with class syntax) all support OOP. Java and C# are strongly class-based, while C++ and Python are multi-paradigm.
2. Encapsulation and Abstraction Interview Questions
Q7. What is encapsulation?
Encapsulation keeps an object's data private and allows access only through public methods, so the class can validate changes and protect its internal state.
class Account:
def __init__(self):
self.__balance = 0 # private
def deposit(self, amt):
if amt > 0:
self.__balance += amt
def get_balance(self):
return self.__balanceQ8. What is abstraction?
Abstraction hides implementation details and shows only what an object does. When you drive a car, you use the steering and brakes without knowing how the engine works internally.
Q9. What is the difference between encapsulation and abstraction?
Encapsulation is about hiding data and controlling access to it, implemented with access modifiers. Abstraction is about hiding complexity and exposing only essential behaviour, implemented with abstract classes and interfaces.
Q10. What are access modifiers?
Access modifiers control visibility: private (only inside the class), protected (the class and its subclasses), public (everywhere), and in Java default/package-private (the same package).
Q11. What are getters and setters?
Getters return the value of a private field and setters change it, often with validation. They are the usual way to implement encapsulation.
Q12. What is an abstract class?
An abstract class cannot be instantiated and may contain abstract methods (without a body) that subclasses must implement, as well as normal methods with code.
Q13. What is an interface?
An interface defines a contract: a set of methods a class must implement. A class can implement several interfaces, which provides a form of multiple inheritance of behaviour.
Q14. What is the difference between an abstract class and an interface?
An abstract class can have state (fields), constructors and implemented methods, and a class can extend only one. An interface mainly declares behaviour, cannot hold instance state, and a class can implement many. Use an abstract class for a shared base with common code, and an interface for a capability shared by unrelated classes.
3. Inheritance Interview Questions
Q15. What is inheritance?
Inheritance lets a child class reuse and extend the attributes and methods of a parent class. A Dog class can inherit from Animal and add a bark method.
class Animal:
def eat(self):
print('eating')
class Dog(Animal):
def bark(self):
print('woof')
d = Dog()
d.eat() # inherited
d.bark()Q16. What are the types of inheritance?
Single (one parent, one child), multilevel (a chain such as A → B → C), hierarchical (several children of one parent), multiple (one child with several parents) and hybrid (a combination). Java supports multiple inheritance only through interfaces; C++ and Python support it with classes.
Q17. What is the diamond problem?
When class D inherits from B and C, which both inherit from A, D may receive two copies of A's members and it is ambiguous which version to use. C++ solves it with virtual inheritance, Python with the method resolution order, and Java avoids it by not allowing multiple class inheritance.
Q18. What is the difference between IS-A and HAS-A relationships?
IS-A is inheritance: a Car is a Vehicle. HAS-A is composition: a Car has an Engine. Composition is often preferred because it is more flexible than deep inheritance.
Q19. What is composition and aggregation?
Both are HAS-A relationships. In composition, the part cannot exist without the whole, such as a House and its Rooms. In aggregation, the part can exist independently, such as a Department and its Teachers.
Q20. What is the super keyword used for?
super refers to the parent class. It is used to call the parent's constructor or an overridden method from the child class.
4. Polymorphism Interview Questions
Q21. What is polymorphism?
Polymorphism means one name with many forms: the same method call behaves differently depending on the object or arguments. It makes code flexible and extensible.
Q22. What is the difference between compile-time and runtime polymorphism?
Compile-time (static) polymorphism is achieved through method overloading and operator overloading, and is resolved by the compiler. Runtime (dynamic) polymorphism is achieved through method overriding and is resolved while the program runs.
Q23. What is method overloading?
Method overloading means several methods in the same class with the same name but different parameter lists (number or types). Changing only the return type is not enough.
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }Q24. What is method overriding?
Method overriding means a child class provides its own version of a method that already exists in the parent, with the same name and parameters. A parent reference pointing to a child object calls the child's version.
class Shape { double area() { return 0; } }
class Square extends Shape {
double s = 4;
@Override double area() { return s * s; }
}
Shape sh = new Square();
sh.area(); // 16.0Q25. What is the difference between overloading and overriding?
Overloading happens in the same class with different parameters and is resolved at compile time. Overriding happens between parent and child with the same signature and is resolved at runtime.
Q26. What is operator overloading?
Operator overloading gives operators such as + or == a custom meaning for user-defined types, for example adding two Complex numbers. C++ and Python support it; Java does not, except for + on strings.
Q27. What is dynamic method dispatch?
Dynamic method dispatch is the mechanism by which a call to an overridden method through a parent reference is resolved at runtime to the actual object's method. In C++ this is implemented with virtual functions and a vtable.
5. Constructors and Object Lifecycle Interview Questions
Q28. What is a constructor?
A constructor is a special method that runs automatically when an object is created and initialises its fields. In Java and C++ it has the class name; in Python it is __init__.
Q29. What are the types of constructors?
Default (no parameters), parameterised (takes arguments) and, in C++ and Java, copy constructors that create an object from another object.
Q30. What is constructor overloading?
Constructor overloading means having several constructors with different parameter lists, so objects can be created in different ways.
Q31. What is a destructor?
A destructor runs when an object is destroyed and releases resources. C++ uses ~ClassName(); Python has __del__; Java relies on garbage collection and try-with-resources instead.
Q32. What is the difference between shallow copy and deep copy?
A shallow copy copies field values, so references still point to the same objects. A deep copy also copies the objects being referenced, so the two copies are fully independent.
Q33. What are static members?
Static fields and methods belong to the class rather than to any object. They are shared by all instances and can be used without creating an object, such as a counter of how many objects were created.
Q34. What is the this keyword?
this refers to the current object. It is used to distinguish fields from parameters with the same name, call another constructor and pass the current object to other methods. Python uses self in the same way.
6. Design and Practical Questions Interview Questions
Q35. What are the SOLID principles?
Single Responsibility (one reason to change), Open/Closed (open for extension, closed for modification), Liskov Substitution (subclasses can replace their parent), Interface Segregation (small, specific interfaces) and Dependency Inversion (depend on abstractions, not concrete classes).
Q36. What is coupling and cohesion?
Coupling is how much classes depend on each other; low coupling is better. Cohesion is how closely related the responsibilities inside one class are; high cohesion is better.
Q37. What is a design pattern? Name a few.
A design pattern is a proven solution to a common design problem. Examples: Singleton (one instance), Factory (creates objects without exposing the exact class), Observer (notify subscribers of changes), Strategy (swap algorithms) and Builder (build complex objects step by step).
Q38. How do you implement a Singleton?
Make the constructor private, keep a static instance, and return it from a static method. In multithreaded code, the instance creation must be thread-safe.
public class Config {
private static final Config INSTANCE = new Config();
private Config() {}
public static Config getInstance() { return INSTANCE; }
}Q39. What are the advantages and disadvantages of OOP?
Advantages: reuse through inheritance and composition, modular code, easier maintenance, data security through encapsulation, and natural modelling of real-world problems. Disadvantages: more design effort, larger programs, and possible performance overhead for simple tasks.
Q40. Design the classes for a library management system.
Typical classes are Book (title, author, ISBN, available), Member (name, ID, borrowed books), Librarian, Loan (book, member, issue date, due date) and Library (collection of books, methods to issue, return and search). Explain the relationships, such as a Member has many Loans, and which methods belong to which class.
How to Prepare for a OOPs Interview
- Explain the four pillars with examples: real-world and in code.
- Know overloading versus overriding: compile-time and run-time polymorphism.
- Compare abstract classes and interfaces: and when to use each.
- Learn SOLID: one example per principle is enough for freshers.
- Design a small system: such as a library or parking lot with classes.
Next steps: apply these concepts in Java interview questions and C++ interview questions, and see all subjects on our interview questions page.
More interview preparation: see all interview questions, including Python, SQL, SQL query, Java, JavaScript, React, C++, .NET, Spring Boot, Node.js, DSA, OOPs, DBMS, operating system, networking, machine learning, data science, AI, Excel, Power BI, DevOps, manual testing, Selenium, cyber security and HR.
Frequently Asked Questions
Why are OOPs questions asked in every interview?
OOP concepts are the foundation of Java, C++, C# and Python development, so interviewers use them to check whether you can design clean, reusable code.
Which language should I use to answer OOPs questions?
Use the language you know best, usually Java, C++ or Python, and give a small code example for each concept.
What is the most asked OOPs question for freshers?
The four pillars with real-world examples, the difference between overloading and overriding, and abstract class versus interface are asked most often.
How should I prepare for OOPs interview questions?
Explain each concept in your own words with a real-world example and a few lines of code, and practise designing classes for simple systems such as a library, ATM or parking lot.