OOPs interview questions test how you organize code using classes and objects. The main ideas are encapsulation, abstraction, inheritance and polymorphism. Freshers explain and code each one. Experienced people explain when not to use them. These 27 answers compare Java, Python and C++ so you can avoid language traps.
How to answer OOPs interview questions
Define the idea, give one small example, and explain its benefit. Use the language on your resume. If you know only Java, the Java OOP interview page goes deeper. This page compares three languages.
| Topic | Java | Python | C++ |
|---|---|---|---|
| Multiple class inheritance | No | Yes | Yes |
| Private access | Compiler rule | Naming habit and name change | Compiler rule |
| Method overloading | Yes | No built-in signature choice | Yes |
| Object cleanup | Garbage collector | Automatic, details depend on Python | Destructors, often with RAII |
OOPs interview questions for freshers: core ideas
Q1. What is object-oriented programming?
30-second answer: It is a way to organize code around objects. An object keeps data and the actions on that data together. A bank account can hold a balance and provide deposit and withdraw actions.
The goal is to make changes easier to control. OOP does not automatically make code good.
Q2. What is the difference between a class and an object?
30-second answer: A class is a design that says what data and actions objects have. An object is one real item made from it in memory.
Student is a class. The object for student Asha is separate from the object for student Ravi. See classes and objects for a full example.
Q3. What is encapsulation?
30-second answer: Encapsulation keeps data and its rules together, and limits unsafe changes from outside. The class controls how its data changes.
class Account {
private int balance;
void deposit(int amount) {
if (amount <= 0) throw new IllegalArgumentException();
balance += amount;
}
int getBalance() { return balance; }
}
Common mistake: Making fields private and adding a setter for every field. If any value can still enter, the object does not protect its rules.
Q4. What is abstraction?
30-second answer: Abstraction shows what something does and hides how it does it. A caller can use payment.pay() without knowing how a card or UPI payment works.
Encapsulation protects internal data. Abstraction makes the visible part simpler. They often work together.
Q5. What is inheritance?
30-second answer: Inheritance lets a child class use and extend a parent class. It shows an "is a" relationship: a Car is a Vehicle.
Do not inherit only to reuse code. If the child cannot safely stand in for the parent, use another design.
Q6. What is polymorphism?
30-second answer: Polymorphism lets one action work in different ways. A Shape can have an area method, and each shape can calculate its own area.
The caller does not need a long list of type checks. Practise it in the Java polymorphism guide.
Q7. What is a constructor?
30-second answer: A constructor prepares a new object. In Java and C++, it has the class name and no return type. In Python, __init__ sets up the new object.
Java adds a no-argument constructor only when you write no constructor. This rule often appears in output questions.
Q8. What is the difference between overloading and overriding?
30-second answer: Overloading means the same method name with different parameters. Overriding means a child gives a new version of a parent's method. In Java, overloading is chosen while compiling, and normal overriding while running.
| Check | Overloading | Overriding |
|---|---|---|
| Parameters | Must differ | Must match the overridden method |
| Parent and child needed | No | Yes |
| Java choice | Compile time | Run time for instance methods |
Changing only the return type is not overloading.
Q9. What does static mean?
30-second answer: In Java, a static member belongs to the class, not one object. All objects share a static field. A static method has no this.
Use a static method for work that needs no object data. Avoid shared static data that many parts can change.
Q10. Abstract class or interface?
30-second answer: Use an abstract class for related classes that share data or code. Use an interface for an ability that different classes can provide. A Java class extends one class but can implement many interfaces.
Modern Java interfaces can have default, static and private methods. "Interfaces contain only abstract methods" is now wrong. Compare them in abstract class vs interface.
Q11. What are association, aggregation and composition?
30-second answer: Association is any link between objects. In aggregation, a part can live without the whole. In composition, the whole controls the part's life.
A team and player show aggregation. An order and its order lines usually show composition.
OOPs interview questions in Java
Q12. Can you override static, private or final methods in Java?
30-second answer: No. A static method can be hidden, a private method is not inherited, and a final method cannot be overridden.
Use @Override so the compiler checks your intention. It catches small parameter mistakes.
Q13. What does this Java code print?
30-second answer: It prints Child P. Java chooses the overridden method from the real object. It chooses a field from the variable's declared type.
class Parent {
String x = "P";
String name() { return "Parent"; }
}
class Child extends Parent {
String x = "C";
@Override String name() { return "Child"; }
}
class Demo {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name() + " " + p.x);
}
}
Interview note: Methods can be overridden. Fields are hidden, not overridden.
Q14. Why do equals and hashCode go together?
30-second answer: If two Java objects are equal, they must have the same hash code. Hash collections use the code to find a group, then equals to find the item.
If you change only equals, a HashSet may keep two equal objects. Unequal objects may still share a hash code.
Q15. How does Java handle multiple inheritance?
30-second answer: A Java class cannot extend two classes. It can implement several interfaces. If two unrelated interfaces give the same default method, the class must provide its own choice.
You can call one with A.super.show(). The answer is therefore not just "Java has no multiple inheritance".
OOPs interview questions: Python and C++
Q16. Does Python have private members?
30-second answer: Python has no strict private members like Java. One starting underscore means internal use. Two starting underscores change the stored name, which helps avoid clashes.
class A:
def __init__(self):
self.__code = 7
print(A()._A__code) # 7
This OOPs interview questions Python trap tests whether you know that the value can still be reached.
Q17. How does Python handle multiple inheritance?
30-second answer: Python follows a method resolution order, called MRO. ClassName.mro() shows it. The first matching method is used.
class A:
def show(self):
return "A"
class B(A):
def show(self):
return "B"
class C(A):
def show(self):
return "C"
class D(B, C):
pass
print(D().show()) # B
D checks B before C because B comes first in its list. More language topics are in Python interview questions.
Q18. What is duck typing?
30-second answer: Duck typing cares about what an object can do, not its class. If an object has the needed method, Python can use it.
A list and a string both work with len because both support that action. Type hints and protocols can document this need.
Q19. What is a virtual function in C++?
30-second answer: A virtual function lets C++ choose a child's version at run time through a base pointer or reference. Without virtual, the call uses the declared base type.
class Shape {
public:
virtual double area() const = 0;
virtual ~Shape() = default;
};
The = 0 makes area pure virtual. So Shape is abstract.
Q20. Why use a virtual destructor in C++?
30-second answer: If you delete a child through a base pointer, the base needs a virtual destructor. Without it, behaviour is undefined, which means the language gives no promise.
Give a base class a public virtual destructor when people may delete children through it. This is one of the most useful OOPs interview questions in C++.
Q21. What is the diamond problem?
30-second answer: A class inherits from two parents that share one grandparent. In C++, this can create two grandparent parts and unclear access. Virtual inheritance gives one shared part.
Python uses MRO. Java avoids the class form but still needs a choice for clashing default interface methods. The Java vs C++ comparison covers other differences.
OOPs questions for experienced developers
Q22. Why choose composition over inheritance?
30-second answer: Composition builds an object using other objects. You can replace a part without changing a family of classes. Inheritance is better when a child truly keeps the parent's promises.
For example, an OrderService can receive a Payment object. Tests can give it a fake payment without making an OrderService child.
Q23. Explain SOLID with a small example.
30-second answer: SOLID is five design guides: one main reason to change, extend without risky edits, safe child types, small interfaces, and dependence on simple promises instead of details.
Suppose an invoice class calculates, saves and emails. Moving save and email to other objects gives each part one clear reason to change.
Q24. Show a broken parent-child design.
30-second answer: If a parent promises a withdrawal and a child always rejects it, code using the parent may fail. The child breaks the Liskov Substitution Principle.
Make a separate ability for accounts that allow withdrawal. A type should promise only what every child can do.
Q25. How would you add a new payment type?
30-second answer: I define a small Payment interface and put each payment type in its own class. I choose the class where the application starts or through a simple registry.
If only two stable choices exist, one clear condition may be enough. Say the cost of extra classes as well as the benefit.
Pro tip: Bring one design you made simpler. Explain the change you expected, what you separated, and what you chose not to build.
Q26. How do you make a class immutable in Java?
30-second answer: An immutable object cannot change after creation. Stop unsafe child classes, keep fields private and final, and provide no setters. Copy any changeable data that enters or leaves.
A final list field does not make the list itself unchangeable. List.copyOf gives an unmodifiable list, but objects inside it may still change. Immutable objects are easier to share between threads.
Q27. When is OOP not the best choice?
30-second answer: A small data conversion may be clearer as a function. If there is no data to protect and no group of behaviours to organize, extra classes add work.
Java, Python and C++ let you mix styles. Choose the form that makes the rule easiest to read, test and change.
How to prepare these answers
Build a small booking app in your language. Add one protected rule, one interface, two different payment types and tests. Then change the payment rule and count how many files you must edit.
On day one, write examples for all four ideas. On day two, practise the language traps. On day three, explain your booking design in five minutes. On day four, ask a friend to give you a changed requirement.
These OOPs interview questions and answers are easiest to remember when they describe code you wrote. Start with OOPs interview questions in Java if your target role uses Java. CodeBegun's Java full stack course teaches the wider application path in class at Madhapur, Hyderabad, and live online.
Frequently Asked Questions
What are the four pillars of OOPs?
Is it OOP or OOPs?
Which language is best for learning OOPs?
Can a class exist without objects?
What OOPs questions are asked to experienced developers?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — View the Java Full Stack curriculum

