โ† Back to list

๐Ÿง  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โ€ฆ

Goyalnandini ยท 2026-06-06 13:47 ยท 0 claps ยท 2.6 min read
#oops-interview-questions #oops-concepts
Open on Medium โ†—
Wiki topics: ๐Ÿ’ป ยท Programming ๐ŸŒ ยท Web Development ๐ŸŽฌ ยท Film & Television

๐Ÿง  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

  1. Encapsulation
  2. Abstraction
  3. Inheritance (IS-A)
  4. 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