Java OOP Interview Notes (2–3 Years Experience)
1. Four Pillars of OOP
Encapsulation
• Bundling data and methods together in a class.
• Restricts direct access to data using access modifiers.
• Achieved using private fields and public getters/setters.
class Account {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
Abstraction
• Hides implementation details.
• Exposes only necessary functionality.
• Achieved using interfaces and abstract classes.
interface Payment {
void pay(double amount);
}
Inheritance
• Establishes an IS-A relationship.
• Promotes code reuse.
class Animal {
void eat() {}
}
class Dog extends Animal {
}
1
Polymorphism
• One interface, multiple implementations.
• Same method behaves differently based on object type.
Animal animal = new Dog();
[Link]();
2. Abstraction vs Encapsulation
Abstraction Encapsulation
Hides implementation details Hides data
Focuses on what object does Focuses on protecting data
Uses interfaces/abstract classes Uses access modifiers
Interview Answer
Abstraction hides complexity from users, while encapsulation protects internal object state.
3. Interface vs Abstract Class
Interface
• Defines a contract.
• Supports multiple inheritance.
• No object state requirement.
Abstract Class
• Provides partial implementation.
• Can have constructors.
• Can contain state and behavior.
When to Use
• Interface → Capability/contract.
• Abstract Class → Shared implementation and state.
2
4. Method Overloading vs Overriding
Overloading (Compile-Time Polymorphism)
class Calculator {
int add(int a, int b) {}
double add(double a, double b) {}
}
Overriding (Runtime Polymorphism)
class Animal {
void sound() {}
}
class Dog extends Animal {
@Override
void sound() {}
}
Difference
Overloading Overriding
Compile-time Runtime
Same name, different parameters Same signature, different implementation
5. IS-A vs HAS-A
IS-A (Inheritance)
class Dog extends Animal {}
Dog IS-A Animal.
HAS-A (Composition)
class Car {
private Engine engine;
}
3
Car HAS-A Engine.
Composition vs Inheritance
Why Composition is Better
Inheritance
class SportsCar extends Car {}
Problems:
• Tight coupling.
• Deep class hierarchies.
• Less flexible.
Composition
class Car {
private Engine engine;
}
Benefits:
• Loose coupling.
• Runtime flexibility.
• Easier testing.
• Reusable components.
Interview Answer
Favor composition over inheritance because composition allows assembling objects from reusable
components and changing behavior without modifying class hierarchies.
Objects Assembled From Reusable Components
Instead of inheriting everything:
class Fish extends Animal {}
4
Use only required behaviors:
class Fish {
private SwimBehavior swimBehavior;
private EatBehavior eatBehavior;
}
Key Idea
Inheritance:
• Gets all parent functionality.
Composition:
• Picks only required functionality.
Association vs Aggregation vs Composition
Association
General relationship.
Teacher ↔ Student
Doctor ↔ Patient
Characteristics:
• No ownership.
• Independent lifecycles.
Aggregation
Weak HAS-A relationship.
class Department {
List<Professor> professors;
}
5
Characteristics:
• Parent contains child.
• Child can exist independently.
Examples:
• Team → Player
• Company → Employee
• Department → Professor
Composition
Strong HAS-A relationship.
class House {
List<Room> rooms;
}
Characteristics:
• Parent owns child.
• Child cannot exist independently.
Examples:
• House → Room
• Order → OrderItem
• Human → Heart
Lifecycle Test
Child survives parent?
YES → Aggregation
Team → Player
Company → Employee
NO → Composition
6
House → Room
Order → OrderItem
No ownership → Association
Teacher → Student
Doctor → Patient
Runtime Polymorphism Internals
Example
Animal animal = new Dog();
[Link]();
Output:
Dog
Compile Time
Compiler checks:
Animal
contains:
sound()
Compiler does not decide implementation.
Runtime
JVM checks actual object:
7
Dog
Calls:
[Link]()
This process is called:
Dynamic Method Dispatch
Dynamic Method Dispatch
Animal a1 = new Dog();
Animal a2 = new Cat();
[Link]();
[Link]();
Runtime:
[Link]()
[Link]()
based on actual object type.
Methods Participating in Runtime Polymorphism
Yes
public void sound()
protected void sound()
default void sound()
8
No
static void sound()
private void sound()
final void sound()
Static Methods vs Runtime Polymorphism
Example
class Animal {
static void sound() {
[Link]("Animal");
}
}
class Dog extends Animal {
static void sound() {
[Link]("Dog");
}
}
Animal animal = new Dog();
[Link]();
Output:
Animal
Why?
Static methods belong to class, not object.
Compiler resolves:
[Link]();
9
at compile time.
No runtime lookup occurs.
Static Methods Are Hidden, Not Overridden
Instance methods:
[Link]()
can override.
Static methods:
[Link]()
only hide parent methods.
Best Practice
Avoid:
[Link]();
for static methods.
Prefer:
[Link]();
10
Frequently Asked Advanced OOP Topics
SOLID Principles
S - Single Responsibility Principle
One class should have one reason to change.
O - Open Closed Principle
Open for extension, closed for modification.
L - Liskov Substitution Principle
Child should replace parent without breaking behavior.
I - Interface Segregation Principle
Clients should not depend on methods they don't use.
D - Dependency Inversion Principle
Depend on abstractions, not implementations.
Liskov Substitution Principle Example
Bad Design:
class Bird {
void fly() {}
}
class Penguin extends Bird {
void fly() {
throw new UnsupportedOperationException();
}
}
Penguin cannot substitute Bird correctly.
11
Dependency Injection
class PaymentService {
private PaymentGateway gateway;
public PaymentService(PaymentGateway gateway) {
[Link] = gateway;
}
}
Benefits:
• Loose coupling.
• Easy testing.
• Easy implementation replacement.
Immutable Objects
final class Employee {
private final String name;
public Employee(String name) {
[Link] = name;
}
}
Benefits:
• Thread-safe.
• Predictable state.
• Safer design.
Builder Pattern
Employee emp = [Link]()
.name("Akhil")
.age(25)
.build();
12
Benefits:
• Readability.
• Avoids large constructors.
• Easy maintenance.
equals() vs hashCode()
Always override together.
Used heavily by:
HashMap
HashSet
Rule:
If two objects are equal according to equals(), they must return the same hashCode().
13