50 Frequently Asked Java OOP Interview Questions (With Answers)
These questions progress from beginner → intermediate → advanced. They cover the concepts interviewers commonly ask in service-based companies, product companies, and startups.
1. What is Object-Oriented Programming (OOP)?
Answer
Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects, which contain both data (fields) and behavior (methods).
Instead of writing code as a collection of independent functions, OOP models real-world entities such as Students, Cars, or Employees as objects.
The primary goals of OOP are:
- Code reusability
- Modularity
- Maintainability
- Scalability
- Security
2. Why is Java called an Object-Oriented language?
Answer
Java is called an Object-Oriented language because most programming is done using classes and objects.
Java supports all major OOP principles:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
However, Java is not purely object-oriented because it also supports primitive data types like int, char, double, and boolean.
3. What are the four pillars of OOP?
Answer
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
These principles help build secure, reusable, and maintainable software.
4. What is a Class?
Answer
A class is a blueprint or template used to create objects.
It defines:
- Variables (state)
- Methods (behavior)
- Constructors
Example:
class Car {
String brand;
void drive() {
System.out.println("Driving");
}
}
No memory is allocated until an object is created.
5. What is an Object?
Answer
An object is a real instance of a class.
Objects occupy memory and contain actual values.
Example
Car car = new Car();
Here,
- Car → Class
- car → Object Reference
- new Car() → Object
6. What is the difference between a Class and an Object?
ClassObjectBlueprintInstanceNo memory allocatedMemory allocatedLogical entityPhysical entityOne class can create many objectsEvery object belongs to one class
7. What is Encapsulation?
Answer
Encapsulation is the process of wrapping data and methods together into a single unit (class) while restricting direct access to the data.
It is achieved using:
- private variables
- public methods
Example
private int salary;
public void setSalary(int salary){
if(salary > 0)
this.salary = salary;
}
8. Why do we use Encapsulation?
Answer
Encapsulation provides:
- Data Security
- Validation
- Better Maintenance
- Loose Coupling
- Easier Debugging
- Better Code Organization
9. Is using Getter and Setter called Encapsulation?
Answer
No.
Getters and Setters are one way to implement encapsulation.
Real encapsulation means exposing only the required behavior.
Example:
Instead of
setBalance()
Use
deposit()
withdraw()
10. What is Data Hiding?
Answer
Data Hiding means restricting direct access to object data.
It is achieved using
private
Example
private int age;
11. Difference between Encapsulation and Data Hiding?
EncapsulationData HidingWraps data + methodsHides only dataAchieved using classesAchieved using access modifiersBroader conceptPart of Encapsulation
12. What is Inheritance?
Answer
Inheritance allows one class to acquire the properties and behaviors of another class.
Example
class Animal{
void eat(){}
}
class Dog extends Animal{
}
Dog inherits eat().
13. Advantages of Inheritance?
Answer
- Code Reusability
- Reduced Code Duplication
- Easier Maintenance
- Method Overriding
- Supports Polymorphism
14. Types of Inheritance supported by Java?
Answer
Java supports
- Single
- Multilevel
- Hierarchical
Java does not support Multiple Inheritance through classes.
15. Why doesn't Java support Multiple Inheritance?
Answer
To avoid the Diamond Problem, where the compiler cannot determine which parent method should be inherited.
Java solves this using Interfaces.
16. What is Method Overriding?
Answer
When a child class provides its own implementation of a parent class method.
Example
class Animal{
void sound(){}
}
class Dog extends Animal{
@Override
void sound(){}
}
17. Rules for Method Overriding?
Answer
- Same method signature
- IS-A relationship
- Cannot reduce visibility
- Return type same or covariant
- Cannot override static methods
- Cannot override final methods
- Cannot override private methods
18. Can Constructors be overridden?
Answer
No.
Constructors are not inherited.
Therefore they cannot be overridden.
19. Can Constructors be overloaded?
Answer
Yes.
Student(){}
Student(String name){}
Student(String name,int age){}
20. What is Polymorphism?
Answer
Polymorphism means Many Forms.
The same method behaves differently for different objects.
There are two types:
- Compile-Time
- Runtime
21. What is Compile-Time Polymorphism?
Answer
Compile-Time Polymorphism is achieved through Method Overloading.
Compiler decides which method to call.
22. What is Runtime Polymorphism?
Answer
Achieved using Method Overriding.
The JVM decides which method to execute during runtime.
23. What is Method Overloading?
Answer
Multiple methods having the same name but different parameter lists.
Example
add(int a,int b)
add(double a,double b)
add(int a,int b,int c)
24. Can we overload by changing only return type?
Answer
No.
Parameter list must change.
25. Difference between Overloading and Overriding?
OverloadingOverridingCompile TimeRuntimeSame ClassParent-ChildDifferent ParametersSame ParametersIncreases readabilityEnables Runtime Polymorphism
26. What is Dynamic Method Dispatch?
Answer
Dynamic Method Dispatch is the mechanism through which overridden methods are resolved at runtime.
Example
Animal animal = new Dog();
animal.sound();
Output
Bark
The reference type is Animal, but the actual object is Dog, so Dog's implementation is called.
27. What is Abstraction?
Answer
Abstraction hides implementation details while exposing only essential functionality.
Example:
A user drives a car without knowing how the engine works.
28. How is Abstraction achieved?
Answer
Using
- Abstract Classes
- Interfaces
29. Can an Abstract Class have Constructors?
Answer
Yes.
They execute whenever a child object is created.
30. Can we create an object of an Abstract Class?
Answer
No.
Abstract classes are incomplete.
31. What is an Interface?
Answer
An interface is a contract that defines what a class must do, without specifying how it should do it.
Example
interface Payment{
void pay();
}
32. Difference between Abstract Class and Interface?
Abstract ClassInterfaceCan have constructorsNo constructorsCan have instance variablesOnly constantsUses extendsUses implementsSupports partial abstractionUsed for full abstraction (conceptually)Single inheritanceMultiple interface implementation
33. Can an Interface have methods with implementation?
Answer
Yes.
Since Java 8:
- default methods
- static methods
Since Java 9:
- private methods
34. What is the this keyword?
Answer
this refers to the current object.
Used for:
- Access current object's fields
- Call another constructor (this())
- Pass the current object
- Return the current object
35. What is the super keyword?
Answer
super refers to the parent class.
Used for:
- Calling parent constructor (super())
- Accessing parent methods
- Accessing parent variables
36. What is Constructor Chaining?
Answer
Constructor chaining is the process of calling one constructor from another using:
- this()
- super()
It avoids code duplication during object initialization.
37. What is the final keyword?
Answer
final can be applied to:
- Variables → Cannot be reassigned.
- Methods → Cannot be overridden.
- Classes → Cannot be inherited.
38. Can a final method be overloaded?
Answer
Yes.
final prevents overriding, not overloading.
39. Can a private method be overridden?
Answer
No.
Private methods are not inherited, so they cannot be overridden.
40. Difference between == and equals()?
==equals()Compares references (or primitive values)Compares object content (if overridden)OperatorMethodDefault for objects checks reference equalityOften overridden in classes like String
Example:
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
41. What is hashCode()?
Answer
hashCode() returns an integer representation of an object.
It is used by hash-based collections like:
- HashMap
- HashSet
- Hashtable
Contract: If two objects are equal according to equals(), they must return the same hashCode().
42. Why should equals() and hashCode() be overridden together?
Answer
Collections like HashMap and HashSet first use hashCode() to locate a bucket and then use equals() to find the exact object.
If only equals() is overridden and hashCode() is not, logically equal objects may end up in different buckets, causing incorrect behavior.
43. What is the Object class?
Answer
Object is the root class of the Java class hierarchy. Every class in Java implicitly extends Object.
Common methods include:
- toString()
- equals()
- hashCode()
- clone()
- getClass()
- wait()
- notify()
- notifyAll()
44. What is Upcasting?
Answer
Upcasting is treating a child object as a parent reference.
Animal animal = new Dog();
It is implicit and safe because every Dog is an Animal.
45. What is Downcasting?
Answer
Downcasting converts a parent reference back to a child type.
Animal animal = new Dog();
Dog dog = (Dog) animal;
It requires an explicit cast and should be done only when the actual object is of the target type.
46. What is the instanceof operator?
Answer
instanceof checks whether an object belongs to a specific class or interface.
Example:
Animal animal = new Dog();
System.out.println(animal instanceof Dog); // true
System.out.println(animal instanceof Animal); // true
It helps avoid ClassCastException before downcasting.
47. What is Composition? Why is it preferred over Inheritance?
Answer
Composition represents a HAS-A relationship.
Example:
class Engine {}
class Car {
private Engine engine = new Engine();
}
A car has an engine.
Composition is generally preferred because it offers:
- Loose coupling
- Greater flexibility
- Better encapsulation
- Easier testing and maintenance
48. What are Association, Aggregation, and Composition?
Answer
- Association: A general relationship where two objects know about each other but are independent. Example: Teacher and Student.
- Aggregation: A weak HAS-A relationship where the child can exist independently of the parent. Example: Department and Professor.
- Composition: A strong HAS-A relationship where the child cannot exist without the parent. Example: House and Room.
49. What are the SOLID Principles?
Answer
SOLID is a set of five object-oriented design principles:
- S – Single Responsibility Principle (A class should have one reason to change.)
- O – Open/Closed Principle (Open for extension, closed for modification.)
- L – Liskov Substitution Principle (Child classes should be substitutable for parent classes.)
- I – Interface Segregation Principle (Prefer small, specific interfaces.)
- D – Dependency Inversion Principle (Depend on abstractions, not concrete implementations.)
These principles help create maintainable and scalable software.
50. What are the most common OOP mistakes beginners make?
Answer
Some common mistakes include:
- Making all fields public, breaking encapsulation.
- Using inheritance only to reuse code instead of modeling an IS-A relationship.
- Confusing method overloading with method overriding.
- Comparing objects with == instead of equals().
- Overriding equals() without overriding hashCode().
- Creating deep inheritance hierarchies instead of using composition.
- Forgetting to use the @Override annotation.
- Not understanding the difference between a reference type and the actual object.
Bonus: Top 10 Tricky Java OOP Interview Questions
- Can a constructor be final, static, or abstract? Why not?
- Why can't private methods be overridden?
- Can an interface extend another interface? Can it extend a class?
- What happens if a class implements two interfaces with the same default method?
- Can an abstract class implement an interface without implementing its methods?
- What is the difference between compile-time binding and runtime binding?
- Why is Object the parent of all classes but not of primitive types?
- Can you override a method and throw a broader checked exception?
- Why does Java support multiple inheritance with interfaces but not with classes?
- How does the JVM implement runtime polymorphism through virtual method dispatch?
