What is POJO and why we use POJO?

🤔 What is a POJO in Java?

POJO stands for Plain Old Java Object.
It means a simple Java class that:

  • Only has variables (fields) to store data
  • Has getter and setter methods to access that data
  • Doesn’t extend any framework classes (like HttpServlet, etc.)
  • Doesn’t implement any special interfaces (unless needed)
  • Has no special rules or annotations

In short: a POJO is just a clean, lightweight class for holding data.

📦 Simple Example of POJO

public class Employee {
    private int id;
    private String name;

    // Constructor
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    // Getters
    public int getId() { return id; }
    public String getName() { return name; }

    // Setters
    public void setId(int id) { this.id = id; }
    public void setName(String name) { this.name = name; }
}

🎯 Why Do We Use POJOs?

  • To store and transfer data (like in APIs, databases, etc.)
  • To keep classes clean and free from unnecessary logic
  • Easy to test, easy to read
  • Used in frameworks like Spring and Hibernate

✅ Interview Key Points

Interview QuestionQuick Answer
What is a POJO?A simple Java object with variables and methods
Does a POJO use inheritance or annotations?No, it’s plain and simple
Where do we use POJOs?In APIs, models, databases, and frameworks
Can we use POJO in Spring Boot?Yes, it’s commonly used for request/response

Leave a Reply

Your email address will not be published. Required fields are marked *