🏛️

Top 50 OOP Interview Questions with Real-Life Examples

A curated set of 50 real OOP interview questions with short answers and relatable real-life examples. Built for software engineering interviews at product and service companies.

50questions
7categories
24beginner
17intermediate
9advanced
Showing 50 of 50 questions

🧠 OOP Fundamentals (7)

Q1.What is Object-Oriented Programming (OOP)?Beginner

A programming paradigm that organizes code around objects — bundles of data (fields) and behavior (methods) that model real-world things. Instead of writing one long sequence of instructions, you describe entities and how they interact.

💡 Real-life example

🚗 A Car class describes attributes (color, fuel) and actions (accelerate, brake). You then create as many car objects as you need from that one blueprint.

Q2.What are the four main pillars of OOP?Beginner

Encapsulation (bundle data + methods, hide internals), Abstraction (expose what, hide how), Inheritance (derive new classes from existing ones), and Polymorphism (one interface, many forms). Together they enable modularity and reuse.

💡 Real-life example

📱 A phone is encapsulated (you tap buttons, internals hidden), abstract (you don't think about radio waves), inheritable (smartphone IS a phone) and polymorphic (the call button works the same on iPhone and Android).

Q3.What is the difference between OOP and procedural programming?Beginner

Procedural focuses on FUNCTIONS that operate on data passed around. OOP groups data + functions together into OBJECTS that own their state. Procedural is 'verb-first' (do this, then that); OOP is 'noun-first' (here's a User, ask it to do things).

💡 Real-life example

🏪 Procedural: a checkout(customer, items) function. OOP: a Customer object owns its cart and knows how to add, remove, and pay.

Q4.What are the advantages of OOP?Beginner

Code reuse via inheritance, easier maintenance (changes localized), better modeling of real-world entities, encapsulation prevents accidental misuse, and clear class boundaries support large-team collaboration.

💡 Real-life example

🏗️ A real-estate app reuses one BaseListing class for Houses, Apartments and Lots — no duplicated logic for shared fields like price, location, status.

Q5.What are the disadvantages of OOP?Beginner

More boilerplate, slower for tiny scripts, easy to over-design with too many classes, and deep inheritance trees become fragile. Pure functions are sometimes simpler than wrapping everything in a class.

💡 Real-life example

🧮 A 20-line script that adds numbers does not need a Calculator class with a single .add() method — a plain function is fine.

Q6.Is OOP suitable for every problem?Intermediate

No. Data pipelines, scripts and numerical computing often work better with functional or procedural styles. OOP shines when you have many entities with stateful behavior — games, GUI apps, business domains.

💡 Real-life example

🎮 A game with Players, Enemies, Items and Quests is naturally OOP. A CSV-to-JSON converter is just a function.

Q7.What are some popular OOP languages?Beginner

Java, C#, C++, Python, Ruby, Kotlin, Swift. Some (Python, JavaScript, Ruby) are multi-paradigm — they support OOP but don't force it. C++ supports OOP, procedural AND generic programming side by side.

💡 Real-life example

🪜 Java was designed OOP-first; JavaScript can do OOP but often uses functional patterns. Same concepts, different flavors.

🏛️ Class & Object (8)

Q8.What is a class?Beginner

A blueprint that defines the structure (fields) and behavior (methods) of objects. A class itself doesn't store data — it describes what objects of that type look like.

💡 Real-life example

🍳 A recipe is a class. The dish you cook from it is the object. The recipe alone isn't food.

Q9.What is an object?Beginner

An instance of a class — a concrete entity in memory with its own values for the class's fields. Many objects can exist from one class, each with independent state.

💡 Real-life example

🐕 Dog is a class. 'Rex', 'Buddy' and 'Max' are three Dog objects, each with their own name and age.

Q10.What is the difference between a class and an object?Beginner

Class is the definition (the plan). Object is the actual thing (the result). One class, many objects. The class lives in code; objects live in memory at runtime.

💡 Real-life example

🏠 A blueprint is a class — one drawing. Houses built from it are objects — each with its own paint, furniture and address.

Q11.What is a constructor?Beginner

A special method that runs automatically when an object is created. Its job is to initialize the object's fields and acquire resources. Often takes parameters to set initial values.

💡 Real-life example

👶 When a baby is born, the constructor sets their name, birthday and assigns a unique ID — once, at creation.

Q12.What is a destructor?Intermediate

A method that runs when an object is destroyed (out of scope or explicitly deleted). It releases resources — closes files, frees memory, disconnects sockets. Not all languages have explicit destructors (Java uses garbage collection).

💡 Real-life example

🔌 When you check out of a hotel room, the destructor returns the key, turns off the lights and signals housekeeping. Cleanup before exit.

Q13.What is the difference between a constructor and a method?Beginner

A constructor has the SAME name as the class, has NO return type and runs only once at object creation. A regular method has any name, can be called many times and explicitly returns values.

💡 Real-life example

🪑 Assembling an IKEA chair = constructor (once, at the start). Sitting in it = method (use it daily).

Q14.What does the `this` keyword refer to?Beginner

Inside a method, `this` refers to the CURRENT object — the instance the method was called on. Used to disambiguate parameter names from fields, and to pass the object to other methods.

💡 Real-life example

📱 When you tap 'Open' on your phone, 'this phone' — not your friend's phone — opens the app. `this` is the device you're holding.

Q15.Can a class have multiple constructors?Intermediate

Yes — through constructor overloading. Multiple constructors with different parameter lists. The compiler picks the right one based on the arguments you pass. Lets you create the same object in different ways.

💡 Real-life example

☕ A Coffee constructor with just size, or size + milk, or size + milk + syrup. Same coffee, different ways to order.

🔒 Encapsulation (7)

Q16.What is encapsulation?Beginner

Bundling data (fields) and the methods that operate on them into a single unit (class), AND restricting direct access to some of them. The object controls how its state is read and changed.

💡 Real-life example

💊 A pill capsule hides the ingredients inside. You swallow it (use the interface) — you don't pry it open to mix the powder yourself.

Q17.What are access modifiers?Beginner

Keywords (public, private, protected) that control where a field or method can be used from. They enforce encapsulation by restricting visibility.

💡 Real-life example

🏠 Your front door is public (anyone can ring). Your bedroom is private (only you). Your guest room is protected (family allowed).

Q18.What is the difference between public, private and protected?Beginner

Public — accessible from anywhere. Private — only inside the same class. Protected — same class plus its subclasses. Use private by default and expose only what callers truly need.

💡 Real-life example

🏧 Your ATM card is public (you can hand it over), your PIN is private (yours only), your account number is protected (you and the bank know it).

Q19.Why use getters and setters?Beginner

To control access to private fields. Getters return the value (often with validation or formatting), setters change it (with validation or side effects like logging). You can later change internals without breaking callers.

💡 Real-life example

🌡️ A thermostat: you don't rewire it. You call setTemperature(72) and it validates the range, logs the change and adjusts the heater.

Q20.What is data hiding?Intermediate

Hiding internal data behind methods, so external code can't tamper with state directly. It's the safety benefit of encapsulation — the object guarantees its own invariants.

💡 Real-life example

🏦 A bank balance can't be set to -1,000,000 directly. You can only call deposit() or withdraw(), which validate the request.

Q21.What is the difference between encapsulation and abstraction?Intermediate

Encapsulation HIDES INTERNAL STATE — how the data is stored. Abstraction HIDES COMPLEXITY — what the operation actually does. Encapsulation is about access control; abstraction is about simplifying the interface.

💡 Real-life example

🚗 Encapsulation: the engine compartment is sealed. Abstraction: you press the gas pedal — combustion, gears and drivetrain are someone else's problem.

Q22.Can you give a real-life example of encapsulation?Beginner

Any device with a public interface and hidden internals: ATMs, microwaves, washing machines, controllers. You interact through buttons and a screen; the wiring, sensors and logic are inaccessible — by design.

💡 Real-life example

🎮 A game controller. You press A, B, X, Y — the chip mapping each button to a signal is sealed inside. You don't (and can't) wire your own.

🧬 Inheritance (8)

Q23.What is inheritance?Beginner

A mechanism where one class (child / subclass) reuses and extends another (parent / superclass). The child gets the parent's fields and methods, and can add new ones or override existing ones.

💡 Real-life example

🐺 A WildWolf class can inherit from Wolf, getting 'hunt' and 'howl' for free, and adding its own 'evadeHumans' method.

Q24.What are the types of inheritance?Intermediate

Single (one parent), multi-level (A → B → C chain), multiple (two parents), hierarchical (one parent, many children) and hybrid (combinations). Not all languages support multiple inheritance (Java doesn't; C++ does).

💡 Real-life example

🌳 Animal → Mammal → Dog is multi-level. Animal having both Dog and Cat as children is hierarchical.

Q25.What is single inheritance?Beginner

A child class has exactly ONE parent class. The simplest and most common form — clear hierarchy, no ambiguity, supported in every OOP language.

💡 Real-life example

🪪 An Employee class extending a Person class. Every employee is one kind of person.

Q26.What is multiple inheritance?Intermediate

A child class has TWO or more parent classes. Powerful but can cause ambiguity (which parent's method to use?). Supported in C++ and Python; not in Java/C# (they use interfaces instead).

💡 Real-life example

🐦 A Bat is a Mammal AND a Flyer. Two parent traits combined in one class.

Q27.What is multilevel inheritance?Intermediate

A chain: class C extends B, and B extends A. Each level adds something. Useful but deep chains become brittle — any base-class change ripples down through every descendant.

💡 Real-life example

🚗 Vehicle → Car → ElectricCar. Each adds capability: Vehicle has wheels, Car has 4 + steering, ElectricCar adds battery and regenerative braking.

Q28.What is the diamond problem and how is it solved?Advanced

When a class inherits from two classes that both inherit from a common ancestor, the child has TWO COPIES of the ancestor's members — ambiguity about which to use. Solved with virtual inheritance (C++) or by avoiding multiple inheritance entirely.

💡 Real-life example

💎 If Bat inherits from both Mammal and Flyer, and both inherit from Animal, Bat has two confused copies of Animal's fields.

Q29.What is the difference between is-a and has-a relationships?Intermediate

'is-a' = inheritance (a Dog IS-A Mammal). 'has-a' = composition (a Car HAS-A Engine). Choose inheritance when the child genuinely IS a kind of the parent; choose composition when it just owns or uses another thing.

💡 Real-life example

🚗 A Car has-an Engine (composition). An ElectricCar is-a Car (inheritance).

Q30.When should you avoid inheritance?Advanced

When the relationship isn't truly 'is-a', when the parent might change frequently (breaking children), or when composition would be more flexible. The principle is 'favor composition over inheritance' — looser coupling, easier to test, less fragile.

💡 Real-life example

🎒 A Backpack has many strap configurations. Rather than NylonStrapBackpack extends Backpack, have Backpack contain a Strap component — swap straps without subclassing.

🎭 Polymorphism (7)

Q31.What is polymorphism?Beginner

'Many forms' — the ability for one interface to invoke different implementations depending on the actual type at runtime, or different method signatures at compile time. Same name, different behaviors.

💡 Real-life example

🔋 A 'charge' button — works the same on phone, laptop or car. Different internals, same interface.

Q32.What is compile-time polymorphism?Intermediate

The compiler decides which method to call based on static type, argument count or types. Achieved via method overloading and operator overloading. Resolved before the program runs — no runtime cost.

💡 Real-life example

➕ `add(1, 2)` vs `add('hi', 'you')` — same name, different signatures, both resolved at compile time.

Q33.What is runtime polymorphism?Intermediate

The decision happens at runtime based on the ACTUAL object type, not the variable type. Achieved via method overriding and virtual functions. The classic OOP feature that enables dynamic behavior.

💡 Real-life example

🐾 `Animal pet = new Dog();` — calling pet.speak() produces 'woof', because at runtime the system knows it's a Dog.

Q34.What is method overloading?Beginner

Defining multiple methods with the SAME NAME but DIFFERENT PARAMETER LISTS (number or types) in the same class. The compiler picks the right one based on the call.

💡 Real-life example

🖨️ `print(int)`, `print(String)`, `print(int[])` — one name, three signatures.

Q35.What is method overriding?Beginner

A child class redefines a method already declared in the parent (with the same signature). At runtime, calls dispatch to the child's version even when accessed through a parent reference.

💡 Real-life example

🦊 Animal has speak() returning '...'. Fox extends Animal and overrides speak() to return 'what does it say?'. Calling speak on a Fox returns the joke.

Q36.What is the difference between overloading and overriding?Intermediate

Overloading = same class, different parameter lists, resolved at compile time (static polymorphism). Overriding = parent/child relationship, same signature, resolved at runtime (dynamic polymorphism).

💡 Real-life example

📦 Overloading: many 'pack' methods with different items. Overriding: a GiftPack subclass changes how 'pack' wraps things.

Q37.What is a virtual function?Advanced

A method declared in the parent that can be overridden by a child, and is dispatched at runtime through the actual object type. In C++ marked with `virtual`. In Java/C#, methods are virtual by default.

💡 Real-life example

🎙️ Animal.speak() is virtual. Calling speak on an Animal-typed variable resolves to Dog's or Cat's version at runtime — depending on what's really there.

🪞 Abstraction (5)

Q38.What is abstraction?Beginner

Exposing only the WHAT, hiding the HOW. The user of an object cares about what it does, not how it does it internally. Abstraction reduces cognitive load and lets implementations evolve without breaking callers.

💡 Real-life example

☕ A coffee machine has a 'brew' button. You don't think about grinding, water temperature or pressure — that's abstracted away.

Q39.What is an abstract class?Intermediate

A class that cannot be instantiated directly — only subclassed. It can mix concrete and abstract (unimplemented) methods. Used to define a contract that child classes must complete.

💡 Real-life example

✏️ A Shape abstract class with an unimplemented area() method. Circle and Square must each provide their own area().

Q40.What is the difference between an abstract class and an interface?Intermediate

Abstract class can have state (fields), constructors and BOTH abstract and concrete methods. Interface (traditionally) is pure contract — no state, only method signatures. Use abstract class for 'is-a partial parent'; use interface for 'can do X'.

💡 Real-life example

🐦 Abstract class Bird (has wings, can lay eggs). Interface Flyable (whatever can fly — birds, planes, bats). A Bat implements Flyable but isn't a Bird.

Q41.What is a pure virtual function?Advanced

A virtual function with NO implementation in the base class (in C++, declared with `= 0`). Forces every concrete subclass to provide its own implementation. Having even one makes the class abstract.

💡 Real-life example

📐 Shape::area() = 0 — every shape must define what its area looks like. Shape itself can't be instantiated.

Q42.Can you instantiate an abstract class?Beginner

No — by design. It exists to be inherited from. You can hold a REFERENCE to a concrete child via the abstract type and call its methods polymorphically. But `new AbstractShape()` is a compile error.

💡 Real-life example

🚫 You can have a Shape variable pointing to a Circle. But you can't create a 'Shape' directly — what would its area even be?

🚀 Advanced & SOLID (8)

Q43.What's the difference between association, aggregation and composition?Advanced

All three are 'has-a' relationships, varying in lifecycle dependency. Association = loose (Teacher knows Student). Aggregation = parent has child, child exists independently (Library has Books). Composition = parent owns child, child dies with parent (House has Rooms).

💡 Real-life example

🏫 A school has students (aggregation — students leave one day). A book has pages (composition — pages can't exist without the book).

Q44.What is a static member?Intermediate

A field or method that belongs to the CLASS, not to any specific instance. Shared across all objects. Accessed via the class name. Useful for constants, counters and utility methods.

💡 Real-life example

🧮 Math.PI in most languages is static — there's no 'instance of Math'. Just one PI for everyone.

Q45.What is a friend function or friend class?Advanced

In C++, a non-member function (or another class) granted access to a class's private and protected members. Breaks encapsulation by design — use sparingly, only when two classes are tightly bound.

💡 Real-life example

🤝 A Wallet class might give its TransactionLog 'friend' access to inspect private balances — they're a closely-cooperating duo.

Q46.What are the SOLID principles?Intermediate

Five design principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion. They make OOP code maintainable, testable and extensible. Coined by Robert C. Martin.

💡 Real-life example

🔑 SOLID is to OOP what 'wash hands' is to cooking — boring, fundamental and skipped at your peril.

Q47.What is the Single Responsibility Principle (SRP)?Intermediate

A class should have ONE reason to change. One responsibility, one purpose. A class that handles authentication, persistence AND email sending will be hell to maintain — split it.

💡 Real-life example

👨‍🍳 A chef cooks, a waiter serves, a cashier handles payments. One restaurant employee doing all three is a nightmare to manage.

Q48.What is the Open/Closed Principle (OCP)?Advanced

Classes should be OPEN for EXTENSION but CLOSED for MODIFICATION. Add new behavior by adding new classes (subclasses, plugins, strategies), not by editing existing code. Reduces regression risk.

💡 Real-life example

🔌 A power strip — you add new appliances by plugging them in. You don't unscrew the strip and rewire it for each new device.

Q49.What is dependency injection?Advanced

Instead of a class creating its dependencies internally, the dependencies are PROVIDED from outside (via constructor, setter or parameter). Makes code testable (swap real with mock) and decoupled.

💡 Real-life example

🚕 A taxi driver isn't built with a specific car bolted to them. You hand them the car (inject it). They can drive any car you provide.

Q50.What is the difference between coupling and cohesion?Advanced

Coupling = how dependent classes are on each other (LOW is good — loose connections). Cohesion = how focused a class is on its purpose (HIGH is good — does one thing well). Aim for low coupling and high cohesion.

💡 Real-life example

🎯 A laser-focused calculator (high cohesion) loosely connected to the UI (low coupling) is great. A swiss-army-knife class that knows about UI, DB and email (low cohesion, high coupling) is a nightmare to maintain.

📚 Want to go deeper?

Pair these questions with real implementation practice from our free courses: