OOPs interview questions, with clear answers
Object-oriented programming comes up in almost every fresher technical round, whatever language you list on your resume. Interviewers want two things: a definition you can say in a sentence or two, and a small example that proves you understand it.
The examples below use Java, but the ideas carry over to C++, C# and Python. Where a language behaves differently, the answer says so.
Sample answers are examples written by Interview Fury. Adapt them to your own experience and say them in your own words.
16 questions on this page
1.What is object-oriented programming?
Why they ask: It's the usual opener before the deeper OOP questions.
OOP is a way of organising a program around objects — bundles of data and the functions that work on that data — instead of around a sequence of procedures. A class describes what an object holds and what it can do; an object is one instance of that class.
For example, a BankAccount class has a balance and methods like deposit() and withdraw(), and each customer's account is an object of that class. The main benefits: code mirrors real things, related logic stays together, and you can reuse and extend code safely.
2.What is the difference between a class and an object?
A class is the blueprint; an object is something built from it. The class Car declares fields like colour and speed and methods like accelerate(). Writing Car myCar = new Car(); creates an object with its own copy of those fields in memory. One class can have many objects, each with its own state.
3.What are the four pillars of OOP?
Why they ask: It's the most common OOP question in fresher interviews.
Encapsulation, abstraction, inheritance and polymorphism.
- Encapsulation: keep data and the methods that change it together, and hide the data — private fields, public methods.
- Abstraction: expose what an object does and hide how it does it — you call
sort()without knowing the algorithm. - Inheritance: one class reuses and extends another —
SavingsAccount extends Account. - Polymorphism: one interface, many forms — the same method call behaves differently depending on the object.
Give a one-line example for each and you've answered it well.
4.What is encapsulation, and why does it matter?
Encapsulation means an object controls its own data: the fields are private and can only be changed through methods that enforce the rules.
class Account {
private double balance;
public void withdraw(double amount) {
if (amount <= 0 || amount > balance) {
throw new IllegalArgumentException("Invalid amount");
}
balance -= amount;
}
}
Nothing outside the class can set the balance to a negative number. It matters because bugs stay contained, and you can change the internals later without breaking the code that uses the class.
5.What is the difference between abstraction and encapsulation?
Abstraction is about design: showing only what an object does — its interface — so users don't need the details. Encapsulation is about implementation: bundling data with its methods and restricting direct access to the data.
The List interface is abstraction: you know you can add() and get(). ArrayList keeping its internal array private is encapsulation. They work together but answer different questions: "what can I use?" versus "who can touch the data?"
Practise saying it, not just reading it
Rehearse these answers out loud with real-time AI help, tailored to your resume and the job description. Free minutes every day — no card required.
Try it free6.What is inheritance, and what types are there?
Inheritance lets a child class reuse and extend a parent class: class Dog extends Animal gets Animal's fields and methods, and can add new ones or override existing ones.
The usual types are single (one parent), multilevel (A → B → C), hierarchical (one parent, several children), multiple (one child, several parents) and hybrid (a mix). Java and C# don't allow multiple inheritance of classes — they use interfaces for that — while C++ and Python do allow it.
7.What is polymorphism? What is the difference between compile-time and runtime polymorphism?
Polymorphism means the same call can do different things.
- Compile-time (static) polymorphism is method overloading: several methods share a name but take different parameters, and the compiler picks one.
- Runtime (dynamic) polymorphism is method overriding: a child class replaces a parent's method, and the version that runs depends on the actual object.
Animal a = new Dog();
a.speak(); // Dog's speak() runs — decided at runtime
8.What is the difference between method overloading and overriding?
Why they ask: Mixing these two up is a common fresher mistake.
- Overloading: same method name, different parameter lists, usually in the same class. Resolved at compile time. The return type alone can't distinguish two overloads.
- Overriding: same name and same parameters in a subclass, replacing the parent's version. Resolved at runtime. The child can't make the method less visible, and in Java you mark it with
@Override.
Example: add(int, int) and add(double, double) are overloads; Dog.speak() replacing Animal.speak() is an override.
9.What is an abstract class, and how is it different from an interface?
An abstract class can't be instantiated. It can have abstract methods (no body) alongside normal methods with code, plus fields and constructors. An interface defines a contract: what a class must be able to do.
The key differences in Java:
- A class can extend only one abstract class, but can implement many interfaces.
- Abstract classes can have constructors and instance fields; interfaces can't hold instance state — only constants.
- Since Java 8, interfaces can also have
defaultandstaticmethods, so the line is thinner than it used to be.
Use an abstract class when related subclasses share state and code; use an interface for a capability that unrelated classes can have, like Comparable.
10.What is a constructor? Can it be overloaded, or private?
A constructor is a special method that runs when an object is created, to set its initial state. It has the same name as the class and no return type.
Yes, constructors can be overloaded — different parameter lists for different ways of creating the object. And yes, a constructor can be private: that's how the Singleton pattern works, and how some classes force you to use a factory method such as Instant.now(). If you don't write any constructor, Java adds a default one with no arguments.
11.What are access modifiers?
They control who can see a class member. In Java:
private— only inside the same class- default (no keyword) — anywhere in the same package
protected— the same package, plus subclasses in other packagespublic— everywhere
A good habit: use the most restrictive modifier that works. Fields private, and methods public only if they're part of the class's intended API.
12.What is the difference between composition and inheritance? When would you prefer composition?
Inheritance is an "is-a" relationship: a Car is a Vehicle. Composition is "has-a": a Car has an Engine, held as a field.
Composition is usually preferred because it couples classes less tightly. You can swap the engine object, test the parts separately, and you don't inherit methods you never wanted. Use inheritance when the child genuinely is a specialised version of the parent and should work anywhere the parent is expected.
13.What is the diamond problem?
It's an ambiguity in multiple inheritance. Classes B and C both inherit from A and override the same method, and D inherits from both B and C. Which version does D get?
C++ solves it with virtual inheritance, and Python with its method resolution order (MRO). Java avoids it for classes by allowing only single inheritance. With interfaces, if two default methods clash, the class must override the method and choose explicitly — for example by calling B.super.method().
14.What are the SOLID principles?
Five design guidelines that keep object-oriented code easy to change:
- Single responsibility: a class should have one reason to change.
- Open/closed: open for extension, closed for modification — add new classes rather than editing working ones.
- Liskov substitution: a subclass must work anywhere its parent is expected.
- Interface segregation: several small interfaces are better than one large one.
- Dependency inversion: depend on abstractions (interfaces), not concrete classes.
For a fresher, explaining two of these with a concrete example usually counts for more than listing all five.
15.What is the difference between static and instance members?
An instance member belongs to each object — every Student has its own name. A static member belongs to the class itself and is shared by all objects, like a static int count that tracks how many students have been created.
Static methods can be called without creating an object, as in Math.max(a, b), but they can't use instance fields directly, because there's no this object.
16.What is the Singleton pattern?
A design pattern that makes sure a class has only one instance, and gives one global way to reach it — used for things like a configuration object or a connection-pool manager. A simple thread-safe version in Java:
public class Config {
private static final Config INSTANCE = new Config();
private Config() {}
public static Config getInstance() {
return INSTANCE;
}
}
It's worth mentioning the downside as well: a singleton behaves like global state, which makes code harder to test.
Prepare with Interview Fury
Rehearse these answers out loud with real-time AI help, tailored to your resume and the job description. Free minutes every day — no card required.
Try it free