Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Guides › Interviews

Top 50 OOP Interview Questions (with Answers)

Classes, the four pillars, overloading vs overriding, abstract classes vs interfaces, composition, SOLID and design patterns — 50 OOP questions with short answers.

OOP comes up in almost every fresher technical interview, whatever language you use. These 50 questions cover the concepts, the tricky comparisons, SOLID and design patterns. Answers are kept short so you can say them in 20–40 seconds; the linked notes have the full explanations and code.

Core concepts

1. What is object-oriented programming? A way of structuring programs as objects that bundle data (fields) and behaviour (methods), modelling real-world things.

2. What is a class and an object? A class is a blueprint; an object is an instance of it with its own data. Car is a class; your red Swift is an object. See Classes, Objects, Constructors and Access Modifiers.

3. What are the four pillars of OOP? Encapsulation, abstraction, inheritance, polymorphism. See Four Pillars of OOP.

4. What is encapsulation? Keeping data private and allowing access only through methods that enforce rules — e.g. a bank account balance that can only change through deposit()/withdraw().

5. What is abstraction? Showing what an object does and hiding how — you call payment.pay() without knowing the UPI internals.

6. Encapsulation vs abstraction? Encapsulation hides data (implementation level, via access modifiers). Abstraction hides complexity (design level, via interfaces/abstract classes).

7. What is inheritance? A child class reuses and extends a parent class — an IS-A relationship (Dog IS-A Animal).

8. Types of inheritance? Single, multilevel, hierarchical, multiple and hybrid. Java supports multiple inheritance only through interfaces; C++ and Python support it with classes.

9. What is polymorphism? "Many forms" — the same call behaves differently depending on the object. Compile-time (overloading) and runtime (overriding).

10. What is the diamond problem? When a class inherits from two classes that both inherit from one base and override the same method — which version is used? Java avoids it (no multiple class inheritance), C++ uses virtual inheritance, Python uses the MRO.

Methods and constructors

11. Overloading vs overriding? Overloading: same name, different parameters, same class, decided at compile time. Overriding: same signature in a subclass, decided at runtime.

12. Can you overload by return type alone? No — the compiler can't tell which method to call from the return type.

13. What is a constructor? A special method with the class name and no return type that initialises a new object; called automatically by new.

14. Types of constructors? Default (no arguments), parameterized, copy constructor, and private constructors (Singleton).

15. Can constructors be inherited or overridden? No. A child constructor calls a parent constructor with super(...) (implicitly if you don't write it).

16. What is constructor chaining? Calling one constructor from another — this(...) in the same class, super(...) for the parent — must be the first statement.

17. What is a destructor? Code that runs when an object is destroyed, to release resources — ~ClassName() in C++. Java has no destructors; the garbage collector frees memory and try-with-resources closes resources.

18. What is a static member? A field or method that belongs to the class, shared by all objects, accessed as ClassName.member.

19. Can a static method use this? No — there is no current object in a static context.

20. What are access modifiers? They control visibility: private, default/package, protected, public (Java); private, protected, public (C++).

Abstraction tools

21. What is an abstract class? A class that can't be instantiated and may contain abstract methods (no body) plus normal methods and fields. See Abstract Class vs Interface.

22. What is an interface? A contract listing methods a class promises to implement. A class can implement many interfaces.

23. Abstract class vs interface — when to use which? Abstract class for closely related classes sharing code and state (IS-A). Interface for a capability shared by unrelated classes (CAN-DO), or when you need multiple inheritance of type.

24. Can an abstract class have a constructor? Yes — it runs when a subclass object is created (called through super).

25. What is a pure virtual function? In C++, virtual void f() = 0; — a function with no body that makes the class abstract.

26. What is a virtual function? In C++, a function declared virtual so that the call is resolved at runtime by the actual object type (dynamic dispatch). Java methods are virtual by default.

27. Why should a base class have a virtual destructor in C++? So that deleting a derived object through a base pointer calls the derived destructor too — otherwise resources leak.

Relationships

28. IS-A vs HAS-A? IS-A = inheritance (Car IS-A Vehicle). HAS-A = composition/aggregation (Car HAS-A Engine).

29. Composition vs aggregation? Both are HAS-A. Composition: the part can't exist without the whole (a Room in a House). Aggregation: the part can exist independently (a Student in a Department).

30. Why prefer composition over inheritance? It's more flexible: behaviour can be swapped at runtime, and deep inheritance chains become fragile. Inherit only for true IS-A relationships.

31. What is association? A general relationship where objects use each other but have independent lifetimes (a Teacher and a Student).

32. What is coupling and cohesion? Coupling = how much classes depend on each other (keep it low). Cohesion = how focused a class is on one job (keep it high).

SOLID

33. What are the SOLID principles? Single responsibility, Open/closed, Liskov substitution, Interface segregation, Dependency inversion. See SOLID Principles.

34. Single responsibility — example? An Invoice class shouldn't also print, save to DB and email itself; split those into separate classes.

35. Open/closed — example? Add a new discount type by adding a new Discount class, not by adding another if to an existing method.

36. Liskov substitution — example? Square extending Rectangle breaks LSP: code that sets width and height separately gets the wrong area for a Square.

37. Interface segregation — example? A Robot shouldn't be forced to implement eat() from a fat Worker interface; split into Workable, Eatable.

38. Dependency inversion — example? OrderService depends on an OrderRepository interface injected through the constructor, not on new MySqlDatabase().

Design patterns (fresher level)

39. What is a design pattern? A proven, reusable solution to a common design problem.

40. Singleton? Only one instance of a class: private constructor + static getInstance(). Used for config, loggers, connection pools.

41. Factory? A method/class that creates objects without the caller knowing the exact class — ShapeFactory.create("circle").

42. Strategy? Encapsulate interchangeable algorithms behind one interface and choose at runtime — payment methods, discount rules.

43. Observer? Objects subscribe to another object and get notified when it changes — event listeners, notifications.

44. Builder? Build complex objects step by step: new User.Builder().name("Asha").age(21).build().

45. Adapter? Wraps an incompatible interface so it works with the one your code expects.

Tricky questions

46. Can you create an object of an interface? No, but you can create an anonymous class or lambda that implements it, and use an interface reference.

47. Is OOP slower than procedural programming? Slightly, in some cases (virtual calls, object overhead), but the difference rarely matters compared to the maintainability gained.

48. What is object slicing (C++)? Assigning a derived object to a base object by value cuts off the derived part. Use pointers or references.

49. What is a friend function (C++)? A non-member function allowed to access a class's private members. Use rarely — it weakens encapsulation.

50. Explain OOP using a real project. Pick your own project: e.g. in a library system, Book, Member and Loan classes (encapsulation), a Payment interface with UpiPayment/CardPayment (abstraction + polymorphism), Member → StudentMember (inheritance). Interviewers love answers tied to your own code — see the parking lot design for a full example.

Practise

More guides