OOP Concepts Interview Questions
40 in-depth OOP interview questions covering encapsulation, inheritance, polymorphism, abstraction, abstract classes, interfaces, and design patterns.
40 in-depth OOP interview questions covering encapsulation, inheritance, polymorphism, abstraction, abstract classes, interfaces, and design patterns. This interview-focused guide covers essential oop concepts interview questions concepts for technical interviews.
Java OOP Concepts Interview Questions
Object-Oriented Programming is the backbone of Java. These 40 questions explore the four pillars, abstract classes vs interfaces, design patterns, SOLID principles, and real-world OOP scenarios.
1. What are the four pillars of Object-Oriented Programming?
- Encapsulation: Bundling data and methods that operate on that data within a single unit (class). Achieved using
privatefields and public getters/setters. - Inheritance: Creating new classes from existing ones. Promotes code reuse. Use
extendskeyword. - Polymorphism: Ability to present the same interface for different data types. Achieved via overloading (compile-time) and overriding (runtime).
- Abstraction: Hiding implementation details and showing only functionality. Achieved via abstract classes and interfaces.
2. What is Encapsulation?
Encapsulation wraps data (variables) and code (methods) together as a single unit. It restricts direct access to object fields by making them private and providing controlled access through public getters/setters.
class BankAccount {
private double balance; // Data hiding
public double getBalance() { return balance; }
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
}
[!TIP] Interview Tip: Encapsulation = Data Hiding + Controlled Access. It's about "what to hide" not just "how to hide."
3. What is Inheritance?
Inheritance is a mechanism where one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass). Use extends keyword. Java supports single inheritance for classes, multiple inheritance for interfaces.
4. Types of Inheritance in Java?
- Single Inheritance: Class B extends Class A
- Multilevel Inheritance: Class C extends Class B extends Class A
- Hierarchical Inheritance: Multiple classes extend same parent class
- Multiple Inheritance (via Interfaces): Class implements multiple interfaces
[!CAUTION] Java does NOT support multiple inheritance with classes (to avoid the Diamond Problem), but supports it through interfaces.
5. What is the Diamond Problem?
The Diamond Problem occurs when a class inherits from two classes that have a method with the same signature. Ambiguity arises about which method to call. Java avoids this by not supporting multiple class inheritance.
6. What is Polymorphism?
Polymorphism means "many forms." The ability of an object to take many forms. Two types:
- Compile-time (Static): Method overloading
- Runtime (Dynamic): Method overriding
7. What is Method Overloading?
Defining multiple methods with the same name but different parameters (number, type, or order). This is compile-time polymorphism. Return type alone is NOT sufficient.
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
8. What is Method Overriding?
Defining a method in a subclass that already exists in the parent class with the same signature. Provides specific implementation. Rules:
- Same name, parameters, return type (or covariant)
- Cannot reduce visibility
- Cannot throw broader checked exceptions
final/staticmethods cannot be overridden
9. Difference between Method Overloading and Method Overriding?
| Feature | Overloading | Overriding |
|---------|------------|------------|
| Binding | Compile-time | Runtime |
| Parameters | MUST differ | MUST be same |
| Return Type | Can differ | Same or covariant |
| Access Modifier | Any | Cannot reduce visibility |
| Class relationship | Same or subclass | Inheritance required |
| static methods | Can overload | Can't override (hidden) |
10. What is the @Override annotation?
@Override tells the compiler that the method is intended to override a superclass method. If no such method exists, the compiler generates an error. It's optional but highly recommended for clarity and error prevention.
11. What is Abstraction?
Abstraction hides complex implementation details and shows only essential features. Achieved through:
- Abstract classes (partial abstraction, 0-100%)
- Interfaces (full abstraction before Java 8, now partial)
12. What is an Abstract Class?
An abstract class is declared with the abstract keyword. It can have both abstract and concrete methods, constructors, and instance variables. It cannot be instantiated directly.
abstract class Vehicle {
abstract void start(); // Abstract method (no body)
void stop() { // Concrete method
System.out.println("Vehicle stopped");
}
}
13. What is an Interface?
An interface defines a contract that implementing classes must follow. Before Java 8: only abstract methods and constants. From Java 8+: can have default and static methods. Java 9+: private methods.
interface Drawable {
void draw(); // Abstract by default
default void describe() { // Default method (Java 8+)
System.out.println("Drawable");
}
}
14. What is the difference between Abstract Class and Interface?
| Feature | Abstract Class | Interface |
|---------|---------------|-----------|
| Keyword | abstract | interface |
| Methods | Abstract + Concrete | Abstract + default + static (Java 8+) |
| Variables | Instance + static | public static final only |
| Constructors | Yes | No |
| Multiple Inheritance | No | Yes |
| Access Modifiers | Any | Methods: public by default |
| When to use | "is-a" relationship, common behavior | "can-do" capability |
15. When should I use an Abstract Class vs an Interface?
- Abstract Class: When classes share common code/state. Use for closely related classes with an "is-a" relationship.
- Interface: When unrelated classes need the same behavior. Use for "can-do" contracts. Since Java 8, interfaces with default methods have blurred this line.
16. Can an abstract class have a constructor?
Yes. Abstract class constructors are called when a subclass object is created. They initialize common fields.
17. Can an abstract class be final?
No. Abstract classes are meant to be extended, while final classes cannot be extended. They are contradictory.
18. Can an interface extend multiple interfaces?
Yes. Interfaces support multiple inheritance. One interface can extend multiple interfaces.
interface A { void methodA(); }
interface B { void methodB(); }
interface C extends A, B { void methodC(); }
19. What are functional interfaces?
A functional interface has exactly one abstract method. They enable lambda expressions. The @FunctionalInterface annotation enforces this rule.
@FunctionalInterface
interface Calculator {
int calculate(int a, int b); // Single abstract method
}
Calculator add = (a, b) -> a + b;
20. What are default methods in interfaces?
Default methods (Java 8+) provide a default implementation in interfaces. They allow adding new methods to interfaces without breaking existing implementations. Implementing classes can override them.
interface Vehicle {
default void start() {
System.out.println("Starting vehicle...");
}
}
21. What are static methods in interfaces?
Static methods in interfaces (Java 8+) belong to the interface itself, not instances. Called via InterfaceName.method(). They cannot be inherited by implementing classes.
22. What is the this keyword?
this refers to the current object instance. Uses:
- Referencing instance variables (when shadowed by parameters)
- Calling another constructor:
this(args) - Passing the current object as a parameter
- Returning the current object from a method
23. What is the super keyword?
super refers to the parent class object. Uses:
- Calling parent class constructor:
super()orsuper(args)— must be first line - Accessing parent class methods:
super.method() - Accessing parent class variables:
super.field
24. What is constructor chaining?
Constructor chaining calls one constructor from another within the same class (using this()) or from the parent class (using super()). Ensures proper initialization hierarchy.
class Person {
Person() { this("Unknown"); } // Calls Person(String)
Person(String name) { this(name, 0); } // Calls Person(String, int)
Person(String name, int age) {
// Actual initialization
}
}
25. What is a static binding vs dynamic binding?
- Static Binding (Early Binding): Resolved at compile time. Used for
static,final,privatemethods, and method overloading. - Dynamic Binding (Late Binding): Resolved at runtime. Used for method overriding. JVM determines which method to call based on the actual object type.
26. What is the IS-A relationship?
IS-A represents inheritance. A subclass IS-A parent class. e.g., Dog IS-A Animal. Used with extends and implements. The instanceof operator checks IS-A relationships.
27. What is the HAS-A relationship?
HAS-A represents composition/aggregation. A class HAS-A reference to another class. e.g., Car HAS-A Engine. It's about objects containing other objects, promoting code reuse without inheritance.
class Car {
private Engine engine; // HAS-A relationship
}
28. What is composition vs inheritance?
- Inheritance:
extends— strong coupling, IS-A. White-box reuse. - Composition: Object as field — loose coupling, HAS-A. Black-box reuse.
[!TIP] Best Practice: Favor composition over inheritance. It's more flexible, easier to test, and avoids the rigid hierarchies inheritance creates.
29. What is association, aggregation, and composition?
- Association: Relationship between two classes (e.g., Teacher and Student)
- Aggregation: Weak HAS-A, child can exist without parent (e.g., Department and Professor)
- Composition: Strong HAS-A, child cannot exist without parent (e.g., House and Room)
30. What is a covariant return type?
A method override can return a more specific type (subtype) than the original method. Introduced in Java 5.
class Animal {
Animal getAnimal() { return this; }
}
class Dog extends Animal {
@Override
Dog getAnimal() { return this; } // Covariant: Dog IS-A Animal
}
31. What is the Object class?
Object is the root class of all Java classes. Every class implicitly extends Object. Key methods: toString(), equals(), hashCode(), getClass(), clone(), finalize(), notify(), wait().
32. Explain the toString() method.
toString() returns a string representation of the object. The default implementation returns ClassName@hashcode. Best practice: always override toString() for meaningful debugging output.
33. Explain the hashCode() method.
hashCode() returns an integer hash code for the object, used in hash-based collections (HashMap, HashSet). Contract: equal objects must have equal hash codes, but unequal objects can have the same hash code (collision).
34. What is the contract between equals() and hashCode()?
- If
equals()returns true,hashCode()MUST return the same value - If
hashCode()returns same value,equals()may or may not return true - Always override BOTH together
- Consistency: Multiple calls on same object return same value
[!IMPORTANT] Golden Rule: If you override
equals(), you MUST overridehashCode(). Otherwise hash-based collections (HashMap, HashSet) will break.
35. What is the instanceof operator?
instanceof tests if an object is an instance of a specific class or interface. Returns boolean. Commonly used for type-safe casting.
36. What is upcasting and downcasting?
- Upcasting: Casting subclass to superclass. Always safe, implicit. (
Dog→Animal) - Downcasting: Casting superclass to subclass. Requires explicit cast. Risky if object isn't actually that type.
Animal animal = new Dog(); // Upcasting (automatic)
Dog dog = (Dog) animal; // Downcasting (explicit)
37. What is dynamic method dispatch?
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime (not compile time). JVM determines the actual object type and calls the appropriate overridden method. This is the basis of runtime polymorphism.
38. What is a marker interface?
A marker interface has no methods or constants. It "marks" a class as having a certain capability. Examples: Serializable, Cloneable, Remote. They signal to JVM/framework that special treatment is needed.
39. Can an interface have private methods?
Yes (Java 9+). Private methods in interfaces allow code reuse between default and static methods without exposing the implementation.
40. What are SOLID principles?
- S - Single Responsibility: One reason to change
- O - Open/Closed: Open for extension, closed for modification
- L - Liskov Substitution: Subtypes must be substitutable for base types
- I - Interface Segregation: Many specific interfaces > one general interface
- D - Dependency Inversion: Depend on abstractions, not concretions
AI Mentor
Confused about "Java OOP interview questions covering abstraction, encapsulation, inheritance, polymorphism, abstract classes, interfaces, and SOLID"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3Which OOP pillar binds data and methods into a single unit?