OOP Interview Questions
Encapsulation, Inheritance, Polymorphism, and Abstraction.
Q1. What are the four main pillars of OOP? Encapsulation, Inheritance, Polymorphism, and Abstraction.
Q2. What is the difference between an abstract class and an interface? An abstract class can have both implemented and abstract methods, along with constructors and fields, but supports only single inheritance. An interface traditionally only declares methods (though modern Java allows default/static methods) and a class can implement multiple interfaces at once.
Q3. Can a constructor be inherited in Java? No, constructors are not inherited by subclasses. However, a subclass constructor can call a parent's constructor using super().
Q4. Why doesn't Java support multiple inheritance of classes? To avoid the "Diamond Problem," where ambiguity would arise if a class inherited the same method from two different parent classes. Java instead achieves similar flexibility using interfaces.
Q5. What is runtime polymorphism, and how does it work internally? Runtime polymorphism occurs through method overriding, where the actual method called depends on the real object type, not the reference type, decided at the moment the program runs.
Q6. Why do we use encapsulation if we could just use public fields directly? Encapsulation allows adding validation logic in setters, restricts direct external modification of internal data, and makes it easier to change internal implementation later without breaking external code that depends on the class.
Q7. What happens if a subclass doesn't implement all abstract methods of its parent abstract class? The subclass must then also be declared abstract itself; otherwise, it results in a compile-time error.
Key Points to Remember
- OOP questions often ask you to explain a concept AND write a small supporting code example — practice both together.
- Interviewers commonly ask "why" a design choice exists (like why Java avoids multiple inheritance), not just "what" it is.
- Be ready to compare related concepts directly, like abstract class vs interface, or overloading vs overriding.