OOPs Interview Questions and OOP Notes
Object-oriented programming fundamentals: inheritance, polymorphism, and the keywords/principles that come up in every C++/Java interview.
18 topics across 4 categories, 72 interview questions
Top 10 OOP interview questions with answers
1.What's the difference between compile-time and runtime polymorphism?
Compile-time polymorphism (like function overloading) is resolved by the compiler before the program runs; runtime polymorphism (like method overriding via virtual functions) is resolved while the program is executing, based on the object's actual type.
Overview of Polymorphism2.What problem does inheritance actually solve?
Code reuse and modeling real "is-a" relationships, so shared behavior lives in one place instead of being duplicated.
Overview of Inheritance3.Why doesn't Java support multiple inheritance of classes?
To avoid the diamond problem, the ambiguity that arises when a class inherits the same member from two different paths. Java sidesteps it by only allowing multiple inheritance through interfaces, which don't carry implementation ambiguity in the same way.
Types of Inheritance4.What is the diamond problem?
When a class inherits the same base class through two or more separate paths, creating duplicate, ambiguous copies of that base class's members.
Virtual Base Classes5.What's required for a method to properly override a superclass method?
The exact same method name, parameter list, and return type (or a covariant subtype of the return type), and it can't reduce the method's accessibility (can't override a public method with a private one).
Method Overriding6.Can two functions be overloads if they only differ by return type?
No, the parameter list itself must differ, the compiler resolves overloads based on the arguments in the call, and return type alone isn't part of that resolution.
Function Overloading7.What makes a class abstract in C++?
Having at least one pure virtual function, a virtual function declared with = 0 and no implementation.
Abstract Classes and Pure Virtual Functions8.What's the actual difference between composition and aggregation?
Ownership and lifecycle: composition means the contained object's lifetime is tied to the container (it can't outlive it); aggregation means the objects have independent lifetimes.
Composition vs Aggregation vs Inheritance9.Explain the Single Responsibility Principle with an example.
A class that handles both saving user data to a database and formatting it for an email report has two reasons to change (a database schema change, or an email format change), violating SRP, it should be split into two focused classes.
SOLID Principles10.Can a static method access non-static (instance) members?
No, not directly, a static method has no specific object to refer to, so it can only call other static members unless it's given a specific instance to work with as a parameter.
The static Keyword
All 72 OOP interview questions
72 questions, grouped by topic. Each one is answered on its topic page, next to the explanation it comes from.
Inheritance
Overview of Inheritance
- What problem does inheritance actually solve?
- What's the difference between inheritance and composition?
- Can a derived class access private members of the base class?
- What happens if you don't call the base class constructor explicitly?
Super and Sub Classes
- Can a subclass have multiple superclasses?
- Does a subclass inherit private fields?
- Can you have an instance of just the superclass?
- If a subclass overrides a method, can it still call the superclass's version?
Types of Inheritance
- Why doesn't Java support multiple inheritance of classes?
- What's the difference between hierarchical and multilevel inheritance?
- Give an example of hybrid inheritance.
- Does C++ allow multiple inheritance, and what's the risk?
Access Specifiers in Inheritance
- What's the default inheritance mode in C++ for `class`, and for `struct`?
- Can a derived class access a private member of its base class?
- When would you use private inheritance instead of composition?
- Does Java have the equivalent of protected or private inheritance?
Constructor and Destructor Order in Inheritance
- In what order do constructors run in an inheritance chain?
- In what order do destructors run?
- What happens if you don't explicitly call a base class constructor?
- Why does a polymorphic base class need a virtual destructor?
Virtual Base Classes
- What is the diamond problem?
- How do you fix the diamond problem in C++?
- Why doesn't Java have this problem?
- Does virtual inheritance have any cost?
Composition vs Aggregation vs Inheritance
- What's the actual difference between composition and aggregation?
- Why is 'favor composition over inheritance' such common advice?
- Give a real example of when inheritance is still the right choice.
- How would you model a 'Car has an Engine' relationship, and why not inheritance?
Keywords
The static Keyword
- Can a static method access non-static (instance) members?
- What's the difference between a static and non-static variable?
- Can you override a static method?
- Give a real use case for a static variable.
The this Keyword
- Why is `this` needed when a parameter has the same name as a field?
- Is `this` available in a static method?
- How does returning `this` enable method chaining?
- What does `this()` (as a call, not just a reference) do inside a constructor in Java?
The const Keyword
- What does marking a member function `const` actually guarantee?
- Why pass a parameter as `const&` instead of by value?
- Can a const method call a non-const method on the same object?
- Is `const` the same as Java's `final`?
The final Keyword
- What are the three things `final` can apply to in Java, and what does each do?
- Why would you mark a class as `final`?
- Does `final` mean the object itself is immutable?
- How does `final` help with thread safety?
Misc
Smart Pointers
- What's the difference between `unique_ptr` and `shared_ptr`?
- When would you use `weak_ptr`?
- What problem do smart pointers actually solve?
- Is there overhead to using `shared_ptr` compared to a raw pointer?
SOLID Principles
- Explain the Single Responsibility Principle with an example.
- What does Open/Closed actually mean in practice?
- Give a real, common violation of Liskov Substitution.
- Why is Dependency Inversion useful for testing?
Polymorphism
Overview of Polymorphism
- What's the difference between compile-time and runtime polymorphism?
- How does runtime polymorphism actually work under the hood?
- Why is polymorphism useful?
- Give a real example of polymorphism you've used or seen.
Function Overloading
- Can two functions be overloads if they only differ by return type?
- Is function overloading resolved at compile time or runtime?
- What happens if a call matches more than one overload ambiguously?
- How is overloading different from overriding?
Method Overriding
- What's required for a method to properly override a superclass method?
- What does `@Override` (or `override` in C++) actually do?
- Can you call the superclass's version of an overridden method?
- What's the difference between overriding and hiding a static method?
Virtual Functions and Inline Virtual Functions
- What happens if you don't mark a base class method as `virtual` and a derived class defines the same method?
- How does a vtable work?
- Is there a performance cost to virtual functions?
- What is a pure virtual function?
Abstract Classes and Pure Virtual Functions
- What makes a class abstract in C++?
- Can an abstract class have a constructor?
- Can a pure virtual function have an implementation?
- What's the difference between an abstract class and a regular class with all virtual methods overridden by default?
Frequently asked questions
Where do these oops interview questions come from?
Each of the 72 questions comes from the "What interviewers actually ask" section of one of 18 OOP topic notes. Every question links to its topic page, where it is answered next to the explanation it tests.
What does the OOP section cover?
18 topics across 4 categories: Object-oriented programming fundamentals: inheritance, polymorphism, and the keywords/principles that come up in every C++/Java interview.
Is it free to read?
Yes. Every article is free to read with no account required. Creating a free DevsUnite account lets you mark topics as read and track your progress across every Core Subjects article.