Interview questions › OOP
OOP interview questions
1. What is Object-Oriented Programming?
Easy
A programming paradigm that organises software around objects — bundles of data (fields) and behaviour (methods) — created from classes. It is built on four pillars: encapsulation, abstraction, inheritance and polymorphism, making code reusable, modular and easier to maintain.
2. What is the difference between a class and an object?
Easy
A class is a blueprint/template that defines fields and methods; it takes no memory for data by itself. An object is an instance of a class created at runtime with its own state in memory. One class → many objects.
3. What is encapsulation?
Easy
Wrapping data and the methods that operate on it into one unit (class) and restricting direct access to the data using access modifiers (private fields + public getters/setters). It protects object state from invalid changes.
4. What is abstraction?
Easy
Showing only the essential features of an object and hiding implementation details. Achieved using abstract classes and interfaces — e.g. you call car.start() without knowing how the engine works.
5. Difference between abstraction and encapsulation?
Medium
Abstraction hides complexity (what an object does vs how) and is a design-level concept. Encapsulation hides data (how state is protected) and is implemented with access modifiers. Abstraction is achieved with interfaces/abstract classes; encapsulation with private fields and public methods.
6. What is inheritance? Name its types.
Easy
A mechanism where a class (child) acquires the properties and methods of another (parent), representing an is-a relationship and enabling reuse. Types: single, multilevel, hierarchical, multiple and hybrid (Java supports multiple inheritance only through interfaces).
7. What is polymorphism?
Easy
The ability of one interface to take many forms. Compile-time polymorphism = method/operator overloading. Runtime polymorphism = method overriding, where the method that runs depends on the actual object type, not the reference type.
8. Method overloading vs method overriding?
Easy
| Overloading | Overriding |
|---|---|
| Same name, different parameters | Same name and parameters in a subclass |
| Same class (or inherited) | Parent–child classes |
| Compile-time polymorphism | Runtime polymorphism |
| Return type may differ | Return type same (or covariant) |
9. What is a constructor? Can it be private?
Easy
A special method that initialises a new object; it has the class name and no return type. Yes, it can be private — used in the Singleton pattern and utility classes to prevent creating objects from outside.
10. What is a copy constructor?
Medium
A constructor that creates a new object as a copy of an existing object of the same class (ClassName(const ClassName &obj) in C++). Java has no built-in copy constructor, but you can write one or use clone().
11. What is a destructor?
Easy
A special method called when an object is destroyed, used to release resources (memory, files). In C++ it is ~ClassName(). Java has no destructors — the garbage collector reclaims memory; use try-with-resources for other resources.
12. What is the `this` keyword?
Easy
this refers to the current object. It is used to distinguish fields from parameters with the same name (this.name = name), to call another constructor (this(...)), and to pass the current object to another method.
13. What is the `super` keyword?
Easy
super refers to the parent class. It calls the parent constructor (super(...), must be the first statement) and accesses overridden parent methods or hidden fields (super.display()).
14. What is a static member?
Easy
A field or method that belongs to the class rather than to any object, shared by all instances. Static methods can be called without creating an object and cannot use this or non-static members directly.
15. What is an abstract class?
Easy
A class declared abstract that cannot be instantiated. It can contain abstract methods (no body) and concrete methods, fields and constructors. Subclasses must implement the abstract methods or be abstract themselves.
16. What is an interface?
Easy
A contract listing methods a class must implement. A class can implement many interfaces. In Java 8+ interfaces can also have default and static methods; variables are implicitly public static final.
17. Abstract class vs interface — when to use which?
Medium
Use an abstract class when related classes share code and state (is-a, e.g. Shape → Circle). Use an interface to define a capability that unrelated classes can have (can-do, e.g. Comparable, Runnable) and when you need multiple inheritance of type.
18. Why does Java not support multiple inheritance of classes?
Medium
To avoid the diamond problem: if two parent classes define the same method, the child would not know which one to inherit. Java allows multiple inheritance through interfaces; if two default methods conflict, the class must override it explicitly.
19. What is the diamond problem? How does C++ solve it?
Hard
When class D inherits from B and C, which both inherit from A, D gets two copies of A's members and calls become ambiguous. C++ solves it with virtual inheritance (class B : virtual public A) so only one shared copy of A exists.
20. What is a virtual function?
Medium
A function declared with virtual in a C++ base class so that calls through a base pointer/reference are resolved at runtime to the derived class's version (dynamic dispatch via the vtable). In Java all non-static, non-final, non-private methods are virtual by default.
21. What is a pure virtual function?
Medium
A virtual function with no implementation in the base class: virtual void draw() = 0;. A class with at least one pure virtual function is abstract and cannot be instantiated.
22. What is a vtable?
Hard
A table of function pointers created by the C++ compiler for each class with virtual functions. Each object stores a hidden pointer (vptr) to its class's vtable, which is used to call the correct override at runtime.
23. What are access modifiers?
Easy
Keywords controlling visibility: private (same class only), default/package-private (same package, Java), protected (same package + subclasses), public (everywhere). They implement encapsulation.
24. What is the difference between composition and inheritance?
Medium
Inheritance is an is-a relationship (Dog is an Animal). Composition is a has-a relationship where a class holds objects of other classes (Car has an Engine). Composition is more flexible and loosely coupled — "favour composition over inheritance".
25. Association vs aggregation vs composition?
Medium
Association: a general relationship between two classes (Teacher – Student). Aggregation: a weak has-a; the part can exist independently (Department has Teachers). Composition: a strong has-a; the part dies with the whole (House has Rooms).
26. What is coupling and cohesion?
Easy
Coupling is how dependent classes are on each other — aim for low coupling. Cohesion is how closely related the responsibilities inside one class are — aim for high cohesion. Together they make code easier to change and test.
27. What is a final class, method and variable?
Easy
A final variable can be assigned once (a constant). A final method cannot be overridden. A final class cannot be extended (e.g. String).
28. What is an immutable object? How to create one?
Medium
An object whose state cannot change after creation (e.g. String). To create one: make the class final, fields private final, set them only in the constructor, provide no setters, and return copies of mutable fields.
29. What is the SOLID principle?
Medium
Five design principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion. They make object-oriented code easier to extend, test and maintain.
30. Explain the Liskov Substitution Principle.
Medium
Objects of a subclass must be usable wherever objects of the parent class are expected, without breaking correctness. If Square extends Rectangle breaks code that sets width and height independently, the hierarchy violates LSP.
31. What is the Singleton pattern?
Medium
A design pattern ensuring a class has only one instance with a global access point. Implemented with a private constructor and a static getInstance() method; use double-checked locking or an enum/holder class for thread safety. Used for loggers, config, DB connection managers.
32. What is the Factory pattern?
Medium
A creational pattern where object creation is delegated to a factory method/class that returns objects of a common interface based on input, so client code doesn't depend on concrete classes (e.g. ShapeFactory.create("circle")).
33. What is the Observer pattern?
Medium
A behavioural pattern where a subject keeps a list of observers and notifies them automatically when its state changes. Used in event listeners, notification systems and MVC (model → views).
34. What is the Strategy pattern?
Medium
Defining a family of interchangeable algorithms behind a common interface and choosing one at runtime — e.g. different payment or pricing strategies injected into a checkout class. It follows the Open/Closed principle.
35. What is dependency injection?
Medium
Providing an object's dependencies from outside (constructor, setter or framework like Spring) instead of the object creating them itself. It reduces coupling and makes classes easy to test with mocks.
36. What is an object's state, behaviour and identity?
Easy
State = values of its fields. Behaviour = what its methods do. Identity = what makes it unique even if the state equals another object (its memory reference).
37. What is shallow copy vs deep copy?
Medium
A shallow copy copies field values, so referenced objects are shared between copies. A deep copy also copies the referenced objects recursively, so the two copies are fully independent.
38. What is operator overloading?
Easy
Giving operators like +, ==, << custom meaning for user-defined types (C++, Python). E.g. adding two Complex objects with a + b. Java does not support it (except + for strings).
39. What is a friend function in C++?
Medium
A non-member function declared with friend inside a class that can access the class's private and protected members. Useful for operator overloading like operator<<, but overuse weakens encapsulation.
40. Can a constructor be inherited or overridden?
Easy
No. Constructors are not inherited and cannot be overridden because they are tied to the class name. A subclass constructor calls the parent constructor using super(...) (or the base initializer list in C++).
41. Can we override a static method?
Medium
No. Static methods belong to the class, so defining the same static method in a subclass hides the parent method (method hiding) — which version runs depends on the reference type, not the object.
42. Can we overload the main method in Java?
Easy
Yes, main can be overloaded with different parameters, but the JVM only calls public static void main(String[] args) as the program entry point.
43. What is an inner class?
Medium
A class defined inside another class. Types in Java: member (non-static) inner class, static nested class, local class (inside a method) and anonymous class. Inner classes can access the outer class's members, including private ones.
44. What is the difference between an abstract method and a concrete method?
Easy
An abstract method has only a signature and no body; subclasses must provide the implementation. A concrete method has a full implementation and can be used directly or overridden.
45. What is the difference between `==` and `.equals()`?
Medium
== compares references for objects (are they the same object?) and values for primitives. .equals() compares logical content if the class overrides it (String, Integer do). Always override hashCode() when overriding equals().
46. What is the purpose of `hashCode()` and `equals()` contract?
Hard
If two objects are equal by equals(), they must have the same hashCode(). Hash-based collections (HashMap, HashSet) use hashCode to find the bucket and equals to find the exact key; breaking the contract makes lookups fail.
47. What is runtime polymorphism with an example?
Easy
class Shape { double area() { return 0; } }
class Circle extends Shape { double r = 1; double area() { return 3.14 * r * r; } }
Shape s = new Circle();
System.out.println(s.area()); // 3.14 — Circle's method chosen at runtime
48. What is the difference between procedural and object-oriented programming?
Easy
Procedural programming (C) organises code as functions acting on separate data, top-down. OOP (Java, C++) organises code into objects combining data and behaviour, offering encapsulation, inheritance and polymorphism — better for large, evolving systems.
49. What is method hiding vs method overriding?
Medium
Overriding applies to instance methods — the runtime object's version is called. Hiding happens when a subclass declares a static method with the same signature as the parent's static method — the version called depends on the reference type at compile time.
50. What are the advantages of OOP?
Easy
Reusability through inheritance and composition, modularity, easier maintenance and debugging, data security through encapsulation, flexibility through polymorphism, and a closer mapping to real-world entities.