๐ง OOPs in Java (Complete Guide with Real Examples + Spring Boot Thinking)
Object-Oriented Programming (OOP) is the foundation of Java and most backend systems like Spring Boot, Amazon, Flipkart, Netflixโฆ
๐ง OOPs in Java (Complete Guide with Real Examples + Spring Boot Thinking)
Object-Oriented Programming (OOP) is the foundation of Java and most backend systems like Spring Boot, Amazon, Flipkart, Netflix architectures.
This guide will connect theory + real-world + interview understanding so you donโt need to revisit basics again.
๐ 1. What is OOP?
OOP is a programming paradigm based on โobjectsโ which contain data (state) and methods (behavior).
๐ง Real-life analogy:
- Car โ object
- Properties โ color, engine, speed
- Behavior โ drive, brake, accelerate
๐งฉ 4 Pillars of OOP
- Encapsulation
- Abstraction
- Inheritance (IS-A)
- Polymorphism (Compile-time + Run-time)
๐ข 1. Encapsulation (Data Hiding)
๐ Meaning:
Wrapping data + methods into one unit and restricting direct access.
๐งโ๐ป Example:
class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
}
๐ง Why?
โ Protect data โ Control access โ Prevent misuse
๐๏ธ Real-world:
- Bank account
- User password
- Payment details
๐ต 2. Abstraction (Hide Complexity)
๐ Meaning:
Show only essential features, hide implementation details.
๐งโ๐ป Example:
interface Payment {
void pay(double amount);
}
class UpiPayment implements Payment {
public void pay(double amount) {
System.out.println("UPI payment done");
}
}
๐ง Why?
โ User doesnโt care HOW it works โ Only WHAT it does matters
๐๏ธ Real-world:
- ATM machine
- Payment gateway
- Spring services
๐ข 3. Inheritance (IS-A Relationship)
๐ Meaning:
One class inherits properties of another class.
๐งโ๐ป Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
๐ง IS-A relationship:
Dog IS-A Animal
๐๏ธ Real-world:
- Employee IS-A Person
- Car IS-A Vehicle
โก Link to polymorphism:
Inheritance enables runtime polymorphism
๐ต 4. Polymorphism (Many Forms)
๐ Meaning:
Same method, different behavior.
โก (A) Compile-time Polymorphism (Overloading)
class MathUtil {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
โ Decided at compile time
๐ฅ (B) Run-time Polymorphism (Overriding)
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Animal a = new Dog();
a.sound(); // Bark
โ Decided at runtime โ IS-A required
๐ง Real Spring Boot Example:
Payment system:
- UPI
- Card
- Wallet
โ Same interface โ Different implementations โ Runtime selection
๐๏ธ 5. HAS-A Relationship (Composition)
๐ Meaning:
One class contains another class.
๐งโ๐ป Example:
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
Engine engine = new Engine();
}
๐ง HAS-A:
Car HAS-A Engine
๐๏ธ Real-world:
- Car HAS-A Engine
- Order HAS-A Items
- User HAS-A Address
๐ Industry rule:
Prefer HAS-A over IS-A (composition over inheritance)
โก 6. IS-A vs HAS-A
FeatureIS-AHAS-ATypeInheritanceCompositionCouplingTightLooseFlexibilityLowHighUsageHierarchyReal systems
๐ง 7. State vs Stateless Objects (IMPORTANT for Spring Boot)
๐ข Stateless:
class PaymentService {
void pay() {}
}
โ No stored data โ Safe for Singleton
๐ต Stateful:
class Cart {
List<String> items = new ArrayList<>();
}
โ Stores data โ Risk in Singleton
๐ 8. Design Patterns connection (REAL WORLD)
๐ง Factory Pattern
Creates objects dynamically
๐ง Example:
Payment p = PaymentFactory.getPayment("upi");
โ No new keyword
โ Runtime decision
โก 9. How OOP is used in Spring Boot
ConceptSpring UsageEncapsulationBeans hide logicAbstractionInterfacesInheritanceController hierarchyPolymorphismStrategy patternComposition (HAS-A)Dependency InjectionSingletonDefault bean scope
๐ง 10. Real System (Amazon/Flipkart)
- Product Service โ Stateless
- Cart Service โ Stateful (external DB/Redis)
- Payment Service โ Polymorphism + Factory
- Order Service โ Workflow orchestration
๐จ FINAL INTERVIEW SUMMARY
OOP is a programming model based on objects and four pillars: Encapsulation (data hiding), Abstraction (hide complexity), Inheritance (IS-A relationship), and Polymorphism (same interface, multiple behaviors). It is widely used in Spring Boot through dependency injection, interfaces, and design patterns.
๐ง FINAL INSIGHT
If you understand this article fully:
โ You understand Java OOP โ You understand Spring Boot design โ You understand real system design basics โ You are interview-ready for OOP questions
๋ฉํ๋ฐ์ดํฐ
- post_id
- d3c2aa8f04a4
- slug
- oops-in-java-complete-guide-with-real-examples-spring-boot-thinking-d3c2aa8f04a4
- url
- https://medium.com/@goyalnandini2001/oops-in-java-complete-guide-with-real-examples-spring-boot-thinking-d3c2aa8f04a4
- canonical_url
- https://medium.com/@goyalnandini2001/oops-in-java-complete-guide-with-real-examples-spring-boot-thinking-d3c2aa8f04a4
- author_url
- https://medium.com/@goyalnandini2001
- status
- ok
- fetched_at
- 2026-06-20 20:29:01