OOPS Interview Questions
OOPS Interview Questions
l
ha
hc
STAGE 1: OOP Fundamentals
Answer: An object is a real-world instance of a class. It has its own state stored in memory
and can use the class’s methods to perform behavior.
C
Class Object
l
Example: Car is the class, myCar is the object.
ha
5. What is the purpose of the new keyword in Java?
Answer:
hc
● Allocates memory on the heap for a new object.
● Calls the constructor to initialize the object.
Car car = new Car(); // 'new' allocates memory and calls constructor
is
N
6. What happens in memory when an object is created?
Answer:
ith
class Marker {}
C
Instance Variables
car1.color = "Red";
l
ha
car2.color = "Blue";
● car1.color is "Red" and car2.color is "Blue" → each object has its own copy.
hc
Instance Methods
● Static variable: A static variable (also called a class variable) is a variable that
belongs to the class rather than to any specific object. It is declared using the static
keyword. All objects of the class share the same copy of a static variable. It is mainly
used to store common properties or values that are shared among all instances of
the class.
Difference Between Static and Instance Variables
Belongs to Object (each object has its own copy) Class (shared by all objects)
Memory Allocated when object is created Allocated only once when class is
Allocation loaded
l
ha
Access Through object reference Through class name (or object)
hc
Purpose Store object-specific data Store class-level/shared data
is
10. Can static methods access instance variables? Why or why not?
Answer:
N
No, static methods belong to the class, not any object. They cannot access instance
variables directly because instance variables belong to objects.
ith
eW
od
C
B. Constructors & Initialization
l
ha
hc
is
Tip: Always mention constructors are implicitly called when new is used.
N
12. What is the difference between a constructor and a method?
Constructor Method
ith
l
ha
3. Copy Constructor (Optional in Java, user-defined)
hc
another object.
● Purpose: To duplicate an existing object.
is
N
ith
Answer:
l
ha
hc
is
N
17. Can constructors be overloaded?
Answer: Yes — just like methods, constructors can have different parameter lists.
ith
18. What happens if you define a parameterized constructor but not a default one?
Answer:
C
l
ha
hc
is
20. What is constructor chaining?
Answer: Constructor chaining = calling one constructor from another (either within the
N
same class using this() or from the parent class using super()).
l
ha
hc
is
23. What is a static variable in Java?
N
Answer:
l
ha
26. What are getters and setters? Why do we use them?
Answer :
hc
● Methods used to access and modify private variables (encapsulation).
● Useful for validation and controlling access.
is
N
ith
eW
Answer:
● ❌ No, because instance variables belong to objects, and static methods belong to
the class.
C
● ❌ No, this refers to the current object, but static methods don’t belong to any object.
l
29. What happens if you don’t use getters/setters and make variables public?
ha
Answer:
hc
● Using getters/setters makes your class maintainable and secure.
●
is
✅ Yes, but not recommended — should access via class name to indicate
N
shared nature.
ith
eW
od
C
STAGE 2: Encapsulation
A. Core Concept
l
ha
hc
32. Why is encapsulation important in OOP?
is
● Protects object state from unintended modifications.
● Makes code more maintainable and flexible.
N
● Supports modular programming and team collaboration.\
34. What is data hiding and how does Java achieve it?
Answers: Data hiding is a key part of encapsulation — it prevents outside classes from
od
accessing the internal state directly. In Java, we achieve this using private access modifiers
on variables and public getters/setters to provide controlled access.
C
Answer: Not fully. While methods and variables are package-private by default, true
encapsulation requires control over visibility, which is only possible with access modifiers like
private or protected. Without them, other classes in the same package can modify variables
directly, breaking encapsulation.
l
ha
37. Is encapsulation only about making variables private?
Ans: No. While making variables private is a key part, encapsulation also involves:
hc
● Providing controlled access through methods
● Possibly making fields immutable for better control
is
38. How does encapsulation improve code maintainability and flexibility?
Ans: Encapsulation allows you to change internal implementation without affecting code that
N
uses the class. For example:
ith
eW
od
Ans: If variables are public, any class can modify them directly. This can:
C
42. Why do we use getters and setters instead of accessing variables directly?
l
● To encapsulate data, hiding implementation.
ha
● To allow future changes without affecting external code.
✅ Yes.
hc
Ans:
44. Can you write a simple Java class that demonstrates encapsulation?
eW
od
C
45. How do access modifiers affect encapsulation?
l
● Typically has private fields, public getters/setters, and no special annotations or logic.
ha
● Represents encapsulation because fields are hidden and accessed only via
controlled methods.
hc
is
N
ith
eW
od
Ans: Encapsulation allows you to hide fields and prevent setters, creating objects that
cannot change state once created.
C
48. How can you make a class immutable using encapsulation?
l
ha
50. Give a real-world example representing encapsulation.
hc
● ATM machine: You don’t see the internal code or money handling, you interact via
buttons/screens only.
● Analogy: Object state is hidden; access controlled via methods (APIs).
is
N
STAGE 3: Inheritance
ith
Example: All vehicles share some features like start(). Instead of writing start() in each
vehicle class, create a parent Vehicle class, and child classes inherit it.
l
ha
53. What is the difference between IS-A and HAS-A relationships?
hc
IS-A Inheritance relationship; subclass is a type of Dog IS-A Animal
superclass
● IS-A → inheritance
● HAS-A → composition
od
l
ha
4. Multiple Inheritance (through interfaces): One class implements multiple
interfaces (because Java classes cannot extend multiple classes).
hc
is
N
ith
Ans: Java does not allow multiple inheritance with classes to avoid ambiguity and the
eW
“Diamond Problem”.
Example of ambiguity:
od
C
Java avoids this problem by disallowing multiple class inheritance but allows
interfaces for multiple inheritance of behavior.
● A class can implement multiple interfaces, thus inheriting behavior from multiple
sources.
● This allows multiple inheritance of type/behavior, but not implementation conflicts.
l
ha
Here, C inherits contracts from both interfaces and provides its implementation.
hc
● super is a reference to the parent class.
● It is used to:
is1. Access parent class methods
2. Access parent class fields
3. Call parent class constructor
N
ith
eW
od
C
Ans: Private members are not visible to subclasses, so they cannot be directly
inherited. However, they can be accessed indirectly via public/protected getters and
setters.
l
ha
59. Can constructors be inherited?
Ans: No, constructors are not inherited, because a constructor is specific to the class.
hc
● Each class must define its own constructor.
● Subclasses can call superclass constructors using super().
is
60. What is constructor chaining in inheritance?
N
Ans:
Output:
Parent constructor
Child constructor
B. Implementation / Coding Questions – Inheritance
Ans: Single inheritance is when one class extends another. The child class inherits all
accessible fields and methods from the parent class.
l
ha
hc
is
N
ith
eW
od
Explanation:
The Child class inherits greet() from Parent. This avoids code duplication and demonstrates
the IS-A relationship: Child IS-A Parent.
C
Ans: Multilevel inheritance is a chain where a class inherits from another, which in turn
inherits from another.
l
ha
hc
is
N
Explanation: Child inherits all methods from Parent and GrandParent, showing code reuse
and logical hierarchy.
ith
Ans: In Java, a subclass can call a parent class constructor using super(). This is
important for initializing parent class fields.
od
C
Output:
Parent Constructor
Child Constructor
l
ha
hc
is
N
Output: Parent static
Explanation: The method is resolved at compile time, not runtime. True overriding requires
ith
instance methods.
65. What happens if a subclass defines a method with the same name but different
eW
66. Can a subclass access protected members of the superclass in another package?
Ans: ✅ Yes, but only through inheritance. Protected members are accessible to
subclasses even in different packages, but not through object references.
l
ha
hc
is
N
ith
encapsulation.
68. Explain what happens when a subclass hides a superclass field with the same
name.
Ans: Field hiding occurs when a subclass declares a field with the same name as in
eW
its parent.
od
C
Explanation: Field hiding does not involve polymorphism; the reference type determines
which field is accessed.
69. How do you prevent a class from being subclassed?
Explanation:This ensures security and immutability in some cases (e.g., String class in
Java).
l
ha
70. Give a real-world example where inheritance improves code reuse and
maintainability.
hc
Ans: Consider a Vehicle superclass with start() and stop() methods. Subclasses like
Car, Bike, and Bus inherit these methods.
is
● No need to write start() in each class → code reuse
● Adding new vehicle types is easy → maintainability
● Polymorphism allows treating all vehicles uniformly:
N
ith
Explanation: This avoids repetitive code and demonstrates hierarchy, reuse, and
eW
polymorphism together.
od
C
STAGE 4: Polymorphism
71. What is polymorphism in Java?
Example:
l
ha
hc
is
N
ith
eW
od
Here, the same reference type Animal behaves differently depending on the actual object,
demonstrating polymorphism.
C
1. Reduces code complexity: One method/interface can handle multiple object types.
2. Enhances flexibility and maintainability: New types can be added without changing
existing code.
3. Supports dynamic behavior: Subclass-specific behavior can replace superclass
behavior at runtime.
4. Encourages loose coupling: Code depends on interfaces or parent classes rather
than concrete implementations.
73. What are the two types of polymorphism in Java?
Ans: This occurs when the compiler decides which method to call at compile time. In
Java, this is mainly achieved by method overloading.
l
ha
hc
is
N
Explanation: The compiler selects the appropriate add method based on parameters — no
ith
Ans: Runtime polymorphism occurs when the method that gets executed is decided at
runtime, not compile time. This is usually achieved using method overriding and
inheritance.
od
C
The JVM determines at runtime which sound() method to call based on the actual object
type (Dog).
76. How does method overloading achieve compile-time polymorphism?
Ans: By defining multiple methods with the same name but different parameters, the
compiler can determine which method to call based on argument types and count.
l
ha
hc
is
Explanation: The compiler resolves the method call during compilation → static
polymorphism.
N
77. How does method overriding achieve runtime polymorphism?
ith
✅
❌
Ans: Yes, constructors can be overloaded (same name, different parameters).
But constructor overloading is compile-time polymorphism, not runtime.
l
Explanation: The compiler decides which constructor to call based on parameters → static
ha
polymorphism.
hc
Ans: No. Private methods belong to the class itself, not the subclass. They are not
visible outside the class, so overriding is impossible.
is
N
ith
eW
Ans: Covariant return type allows the overridden method in subclass to return a
subtype of the superclass method’s return type.
C
81. What is the difference between method overloading and method overriding?
Feature Method Overloading Method Overriding
l
ha
Return Type Can vary (but if types are same, Must be same or covariant return
no issue). type.
hc
increase).
Exceptions Can throw any exception. Must not throw broader checked
is exceptions than superclass.
Example – Overloading:
N
class Calculator {
ith
Example – Overriding:
class Animal {
void sound() { System.out.println("Some sound"); }
}
od
82. Can private, final, or static methods be overridden? Why or why not?
Ans: Private methods → ❌ Cannot be overridden because they are not visible to
subclasses. They belong to the class itself.
❌
❌
● Final methods → Cannot be overridden because final prevents modification.
● Static methods → Cannot be overridden. Defining a static method with the same
name in a subclass hides the superclass method; it’s method hiding, not overriding.
class Parent {
private void privateMethod() {}
final void finalMethod() {}
static void staticMethod() {}
}
class Child extends Parent {
// All above cannot be overridden
}
l
Ans: 1. Compile-time polymorphism → Early binding
ha
○ The compiler decides which method to call based on method signature
(overloading).
hc
2. Runtime polymorphism → Late binding
○ JVM decides which method to call based on actual object type at runtime
is (overriding).
84. How does Java resolve which method to call at runtime in case of
overriding?
eW
● Java uses dynamic method dispatch: the JVM checks the actual object type (not
reference type) at runtime and invokes the overridden method of the subclass.
● Reference type determines what methods are visible, but object type determines
which implementation is executed.
od
Ans: Dynamic Method Dispatch (DMD) is the mechanism by which Java resolves
overridden method calls at runtime.
l
ha
Explanation:
hc
● Animal a reference → visible methods
● Dog object → actual method executed at runtime
is
87. How does polymorphism improve code maintainability and flexibility in large
applications?
N
● Decouples code from implementation: Code depends on interfaces or superclass, not
ith
specific implementations.
● Easy to extend: New classes can be added without changing existing logic.
● Reduces code duplication: Same method calls can work for multiple object types.
● Supports runtime flexibility: Behavior can change dynamically based on object type.
eW
class UPI implements Payment { public void pay(double amt){ System.out.println("Paid via
UPI"); } }
Virtual Keyword Not needed, all non-final Required to mark a method as virtual
instance methods are virtual for runtime polymorphism
Multiple Not allowed for classes, only Allowed for classes, can lead to
l
Inheritance via interfaces diamond problem
ha
Explanation: Java simplifies runtime polymorphism by making all instance methods virtual
by default.
hc
89. Can you override a superclass method and change its access modifier? What are
is
the rules?
✅ allowed
class Parent { protected void show() {} }
class Child extends Parent { public void show() {} } //
● Pressing the “Power” button sends the same signal, but each device responds
differently: TV turns on, AC starts, Fan spins.
Explanation:
● Dynamic method dispatch decides what happens when the “Power” button is pressed
→ runtime polymorphism.
STAGE 5: Abstraction
Ans: Abstraction is the OOP concept of hiding implementation details and showing only
essential functionality to the user. It allows you to focus on what an object does, not how it
does it.
● Example analogy: When you drive a car, you just press the accelerator, brake, or
steering; you don’t need to know how the engine works internally.
● In Java, abstraction is implemented using abstract classes and interfaces.
l
ha
92. How is abstraction different from encapsulation?
hc
Purpose Hides implementation details, Hides internal state and protects
shows only functionality data from direct access
Example Vehicle class exposes start() but Car class makes speed private and
hides engine details provides getSpeed()
eW
95. Can an abstract class have a constructor? If yes, why is it useful?
96. Can an abstract class have both abstract and non-abstract (concrete) methods?
✅ Yes.
l
● Abstract methods define what subclasses must implement.
ha
● Concrete methods provide default behavior that can be reused.
Example: Vehicle has start() abstract, but stop() concrete. Subclasses implement start(),
and inherit stop().
hc
97. Can you instantiate an abstract class? Why or why not?
99. Can interfaces have method implementations in Java? If yes, how (default, static,
private methods)?
Ans: ✅ Yes:
C
100. What is the difference between an abstract class and an interface in Java?
Feature Abstract Class Interface
Multiple ❌ No ✅ Yes
inheritance
Methods Abstract + concrete Default, static, abstract (all methods are public
by default)
l
ha
Access modifiers Any Methods: public by default, can have private
(Java 9+)
hc
101. Can a class extend multiple abstract classes? Why or why not?
Ans: ❌ No, Java does not support multiple inheritance of classes (abstract or concrete)
is
to avoid diamond problem.
inheritance in classes?
Ans: ✅ Yes.
eW
103. Can an interface extend another interface? Can it extend multiple interfaces?
od
Ans: ✅ Yes.
● One interface can extend multiple interfaces.
C
Ans: ✅ Yes.
● An abstract class can implement an interface partially (provide some method
implementations) and leave the rest to subclasses.
105. What is the purpose of default methods in interfaces?
Ans: ❌ No.
● Static methods belong to interface itself, not instances.
● They cannot participate in polymorphism or be overridden.
l
ha
107. Can a private method exist in an interface? How is it used?
hc
Ans:
108. Can you declare variables in an interface? What are their default modifiers?
N
Ans: Yes, all variables in an interface are implicitly:
ith
○ public
○ static
○ final (constant)
eW
Ans:
od
110. How does abstraction improve code maintainability, flexibility, and scalability in
large projects?
Ans: Association defines a relationship between two separate classes that are
connected through their objects.
It represents a “uses-a” or “works-with” relationship.
Example: A Teacher teaches multiple Students. Both can exist independently.
Ans: Aggregation is a special form of association that represents a “has-a” relationship with
l
shared ownership.
ha
● Both objects can exist independently.
● Example: A Library has Books. If the Library is deleted, the Books can still exist.
hc
Difference:
● Example: A House has Rooms. If the House is destroyed, the Rooms are too.
● Lifecycle: A child object’s lifecycle is bound to the parent.
eW
Difference:
114. Can you give real-world examples of association, aggregation, and composition?
l
Key Difference:
ha
● IS-A: established via extends (inheritance)
● HAS-A: established via object references (composition/aggregation)
hc
116. When should you prefer composition over inheritance?
●
●
is
You want flexibility and loose coupling.
Behavior should be changed at runtime (by replacing components).
N
● You don’t want to expose all superclass behavior.
● Example: Instead of inheriting Engine, Car has an Engine object → you can easily
ith
Ans: Dependency means one class depends on another to perform its function.
It’s a temporary association (used only when needed).
Example: Driver depends on Car to drive → if car changes, driver’s behavior may change.
Ans: ✅ Yes.
A class can have multiple associations with another class if they represent different roles
or relationships.
Example:
So, multiple associations between the same classes are allowed if their purpose differs.
l
ha
120. Can aggregation exist without a “has-a” relationship?
Ans: ❌ No.
hc
Aggregation always implies a “has-a” relationship because it represents ownership or
whole-part structure.
If two classes are only loosely connected without ownership, that’s a simple association,
not aggregation.
is
✅ Summary Table for Quick Recall:
N
ith
121. Can a constructor be declared final, static, or abstract? Why or why not?
Ans: No.
● final: Constructors are not inherited, so making them final makes no sense.
● static: Constructors are called to create objects, but static methods belong to the
class, not objects.
● abstract: Constructors must have a body, while abstract methods don’t.
l
ha
122. Can we use this() and super() together in the same constructor? Why or why not?
hc
Ans: No. Both this() and super() must be the first statement inside a constructor.
Java doesn’t allow two first statements, so you can only call one — either another
constructor of the same class (this()) or the parent constructor (super()).
is
123. What is the difference between a static block and an instance initializer block?
N
Feature Static Block Instance Initializer Block
loaded
Example:
od
class Demo {
static { System.out.println("Static block"); }
C
{ System.out.println("Instance block"); }
}
Static block
Instance block
Instance block
124. What happens if you declare a static variable inside a method?
125. Can a class be both abstract and final at the same time?
Ans: No.
l
ha
These are opposite in meaning, so Java doesn’t allow both together.
hc
Ans: No.
Static methods belong to the class, while abstract methods are meant to be overridden by
subclasses.
Since static methods cannot be overridden, combining static and abstract makes no sense.
is
127. Can a static method access non-static data members or methods?
N
Ans: No, because static methods belong to the class, not to any particular object.
They can only access static members directly.
ith
Example:
eW
class Test {
int x = 5;
❌
static void show() {
// System.out.println(x); Not allowed
✅
Test obj = new Test();
od
System.out.println(obj.x); // Allowed
}
}
C
128. What happens if you call this() or super() from a normal method (not a
constructor)?
Ans: You cannot call this() or super() from a normal method — it causes a compile-time
error. These calls are only allowed inside constructors, and must be the first statement.
✅ Why?
Because this() and super() are used to control object construction — they determine which
constructor is executed first in the chain. Once the object is constructed, there’s no need (or
logical sense) to invoke them again.
129. Can you override a method and make it static in the subclass?
Ans: No.
A static method cannot be overridden, because static methods are class-level — not tied to
an object.
However, you can redeclare a static method in the subclass — this is called method hiding,
not overriding.
Example:
class Parent {
static void show() { System.out.println("Parent"); }
}
l
ha
class Child extends Parent {
static void show() { System.out.println("Child"); }
}
hc
Here, calling Parent.show() prints Parent, and Child.show() prints Child,
but polymorphism does not apply — method resolution happens at compile time.
That means they behave like constants shared by all implementing classes.
ith
You cannot make them non-static or non-final, nor can you reassign their values in
implementation classes.
✅ Summary Table:
eW
constructors
Ans: Method Overloading happens when multiple methods in the same class have the same
name but different parameters (type, number, or order).
Method Overriding occurs when a subclass provides a new implementation for a method
that is already defined in its superclass.
l
Time e
ha
Parameters Must differ Must be same
hc
covariant
Ans:
ith
✅
time.How it is achieved:
eW
✅
How it is achieved:
Achieved through Method Overriding (same method name and parameters in parent and
child classes).
l
ha
hc
is
N
ith
time runtime
Binding
Ans: Static Binding is determined at compile-time (used by static, private, and final
methods).
Dynamic Binding is determined at runtime for overridden instance methods.
l
ha
Polymorphism No Yes
hc
Ans: Both use the same name with different parameters, but constructors initialize objects
while methods perform actions.
Aspect
is Constructor Overloading Method Overloading
135. Difference between Constructor and Method Overriding (and why constructors
eW
can’t be overridden)
Ans: Constructors cannot be overridden because they are not inherited. Each class has its
own constructors that are called via super().
od
Inherited No Yes
C
Overridable No Yes
Ans: Instance Methods are tied to objects and participate in runtime polymorphism.
Static Methods are tied to the class and resolved at compile-time.
l
ha
137. Difference between Superclass Reference and Subclass Reference Behavior in
Runtime Polymorphism
hc
Ans: A superclass reference can refer to a subclass object, enabling runtime polymorphism.
At runtime, the actual object’s overridden method is executed.
Example: is
Parent p = new Child();
N
p.show(); // Executes Child’s show()
ith
Ans: Abstract Class provides partial abstraction (can have both abstract & concrete
methods).
C
l
ha
Ans: Interface Inheritance allows multiple inheritance and defines only contracts.
Class Inheritance allows single inheritance and includes data and behavior.
hc
Aspect Interface Inheritance Class Inheritance
Members
is Methods only Methods + Variables
N
SHOW SOME LOVE BY FOLLOWING CodeWithNishchal:
ith
○ Instagram: https://www.instagram.com/codewithnishchal/
○ LinkedIn: https://www.linkedin.com/in/nishchal-muradia/
○ YouTube: https://www.youtube.com/@CodeWithNishchal
eW