
11. Java OOP
This section focuses on core Object-Oriented Programming concepts in Java, including encapsulation, inheritance, polymorphism, abstraction, interfaces, composition, SOLID principles, and how OOP supports scalable and maintainable backend application design.
1. What is OOP?
OOP stands for Object-Oriented Programming.
It is a programming paradigm where software is designed using objects instead of only functions and procedures.
An object contains:
State (fields/data)
Behavior (methods)
Example:
public class User {
private String name;
public void login() {
}
}Here:
name → state
login() → behaviorOOP helps organize large applications into reusable and maintainable components.
Benefits of OOP:
Code reuse
Scalability
Maintainability
Modularity
Better abstraction
Easier testingModern enterprise Java applications heavily rely on OOP principles.
2. What are the four principles of OOP?
The four core principles are:
Principle | Purpose |
|---|---|
Encapsulation | Protect internal state |
Inheritance | Reuse existing behavior |
Polymorphism | Multiple implementations |
Abstraction | Hide implementation details |
These principles help create flexible and maintainable systems.
3. What is encapsulation?
Encapsulation means hiding internal implementation details and controlling access to object data.
Example:
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}The field:
private double balance;cannot be modified directly from outside the class.
Advantages:
Data protection
Controlled modification
Validation support
Reduced coupling
Better maintainabilityEncapsulation is usually implemented using:
private fields
public methods
getters/setters4. What is inheritance?
Inheritance allows one class to acquire properties and behavior from another class.
Example:
public class Animal {
public void eat() {
}
}public class Dog extends Animal {
}Dog automatically inherits:
eat()Benefits:
Code reuse
Hierarchical relationships
Reduced duplicationInheritance models an:
"is-a"relationship.
Example:
Dog is an Animal5. What is polymorphism?
Polymorphism means one interface or method can have multiple implementations.
Example:
public interface PaymentService {
void pay();
}Implementations:
public class PaypalService implements PaymentService {
}
public class StripeService implements PaymentService {
}Now:
PaymentService service;can reference different implementations.
Benefits:
Flexibility
Loose coupling
Extensibility
Cleaner architecturePolymorphism is heavily used in Spring Framework.
6. What is abstraction?
Abstraction means exposing only essential behavior while hiding implementation details.
Example:
public interface PaymentService {
void pay();
}Users know:
pay()exists.
But they do not know internal payment logic.
Advantages:
Reduced complexity
Cleaner APIs
Better maintainability
Loose couplingAbstraction is commonly implemented using:
Interfaces
Abstract classes7. Difference between abstraction and encapsulation?
This is a very common interview question.
Encapsulation
Focuses on:
Protecting internal stateExample:
private fieldsEncapsulation hides data.
Abstraction
Focuses on:
Hiding implementation complexityExample:
interface PaymentServiceAbstraction hides implementation details.
Main difference:
Encapsulation | Abstraction |
|---|---|
Hides data | Hides complexity |
Focus on protection | Focus on simplification |
Implemented using access modifiers | Implemented using interfaces/abstract classes |
8. Difference between inheritance and composition?
Inheritance:
is-a relationshipComposition:
has-a relationshipExample inheritance:
Dog is an AnimalExample composition:
Car has an EngineComposition example:
public class Car {
private Engine engine;
}Composition builds objects from smaller components.
9. Why is composition often preferred over inheritance?
Inheritance creates tight coupling between parent and child classes.
Problems:
Fragile hierarchy
Difficult maintenance
Limited flexibilityComposition is more flexible because behavior can be changed dynamically.
Example:
private PaymentStrategy strategy;Advantages of composition:
Loose coupling
Better flexibility
Easier testing
Better maintainabilityModern design principles often prefer:
Favor composition over inheritance10. What is method overloading?
Method overloading means multiple methods have the same name but different parameters.
Example:
public void print(String s) {
}
public void print(int n) {
}Compiler decides which method to call based on argument types.
Overloading happens at:
Compile time11. What is method overriding?
Method overriding happens when a subclass provides a new implementation of a parent method.
Example:
class Animal {
void sound() {
}
}class Dog extends Animal {
@Override
void sound() {
}
}Runtime decides which implementation executes.
12. Difference between overloading and overriding?
Overloading | Overriding |
|---|---|
Same method name | Same method signature |
Different parameters | Same parameters |
Compile-time polymorphism | Runtime polymorphism |
Usually same class | Parent-child relationship |
13. Can we override static methods?
No.
Static methods belong to the class, not the object.
Example:
class A {
static void test() {
}
}class B extends A {
static void test() {
}
}This is method hiding, not overriding.
Method selection happens at compile time.
14. Can we override private methods?
No.
Private methods are not visible to subclasses.
Example:
private void test() {
}Subclasses cannot access or override it.
15. Can we override final methods?
No.
final means the method cannot be changed by subclasses.
Example:
final void process() {
}Attempting override causes compilation error.
16. What is interface?
An interface defines a contract of behavior.
Example:
public interface PaymentService {
void pay();
}Interfaces define:
What should be donenot:
How it is doneInterfaces support abstraction and polymorphism.
17. What is abstract class?
An abstract class is a partially implemented base class.
Example:
public abstract class Animal {
abstract void sound();
void sleep() {
}
}Abstract classes can contain:
Abstract methods
Concrete methods
Fields
Constructors18. Difference between interface and abstract class?
Interface | Abstract Class |
|---|---|
Contract | Partial implementation |
Multiple inheritance supported | Single inheritance only |
Usually stateless | Can contain state |
Focus on capability | Focus on shared base behavior |
Interfaces provide flexibility.
Abstract classes provide reusable base logic.
19. When should you use interface?
Use interface when:
Multiple implementations exist
Loose coupling is important
Behavior contract is neededExample:
PaymentService
NotificationService
StorageServiceSpring heavily uses interfaces for flexibility and dependency injection.
20. When should you use abstract class?
Use abstract class when subclasses share common behavior or state.
Example:
abstract class BaseEntity {
Long id;
LocalDateTime createdAt;
}Good for shared reusable logic.
21. Why does Spring often use interfaces?
Spring promotes loose coupling.
Example:
UserService service;instead of:
UserServiceImpl service;Benefits:
Easier testing
Flexible implementations
Cleaner architecture
Dynamic proxies
AOP supportSpring proxies often work naturally with interfaces.
22. What is loose coupling?
Loose coupling means components depend minimally on each other.
Example:
PaymentService service;instead of:
PaypalService service;Advantages:
Easier maintenance
Flexible implementation changes
Better testing
Lower dependency impactDependency Injection strongly supports loose coupling.
23. What is high cohesion?
High cohesion means a class has a focused responsibility.
Good example:
UserService handles user logic onlyBad example:
UserService handles:
payments
emails
PDF generation
authentication
inventoryHigh cohesion improves:
Readability
Maintainability
Testability
Design quality24. What is dependency injection?
Dependency Injection means dependencies are provided externally instead of created manually.
Without DI:
private UserRepository repo = new UserRepository();With DI:
public UserService(UserRepository repo) {
this.repo = repo;
}Spring injects dependencies automatically.
Benefits:
Loose coupling
Easier testing
Better maintainability
Cleaner architecture25. What is SOLID?
SOLID is a set of five object-oriented design principles.
Principle | Meaning |
|---|---|
S | Single Responsibility Principle |
O | Open/Closed Principle |
L | Liskov Substitution Principle |
I | Interface Segregation Principle |
D | Dependency Inversion Principle |
Single Responsibility Principle
A class should have only one reason to change.
Open/Closed Principle
Classes should be:
Open for extension
Closed for modificationLiskov Substitution Principle
Subclasses should safely replace parent classes without breaking behavior.
Interface Segregation Principle
Do not force classes to implement methods they do not need.
Prefer smaller focused interfaces.
Dependency Inversion Principle
High-level modules should depend on abstractions, not concrete implementations.
Example:
PaymentServiceinstead of:
PaypalServiceSOLID principles help create scalable and maintainable enterprise applications.
