Java Bean vs POJO (Plain Old Java Object)

In this post, we will learn the difference between Java Bean and POJO (Plain Old Java Object) in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

Difference between Java Bean and POJO (Plain Old Java Object) in Java

Feature Java Bean POJO (Plain Old Java Object)
Definition A specific type of POJO that follows certain conventions and guidelines. A simple Java class that encapsulates data without any special requirements.
Constructor Must have a no-argument constructor (default constructor). Can have any type of constructor, including parameterized constructors.
Getter and Setter Methods Typically provides public getter and setter methods following the "getPropertyName" and "setPropertyName" naming conventions. May or may not provide getter and setter methods; properties can be accessed directly as well.
Interfaces May implement various interfaces like Serializable, Cloneable, etc. based on its requirements. No specific interfaces need to be implemented.
Special Behavior Supports property change listeners, allowing other objects to be notified when a property changes. Does not have any special behavior; simple data representation or storage.
Usage Often used in GUI frameworks (JavaFX, Swing) where properties are bound to UI components. Used for simple data representation, business logic, data transfer objects (DTOs), and general-purpose classes.
Let's create examples for both Java Bean and POJO (Plain Old Java Object) to understand their differences. 

Java Bean Example: 

A Java Bean is a class that follows specific conventions to be used as a reusable software component. It must have a default (no-argument) constructor, private fields with public getter and setter methods (accessors and mutators), and should implement the Serializable interface (if needed). 

Here's an example of a Java Bean:
import java.io.Serializable;

public class PersonBean implements Serializable {
    private String name;
    private int age;

    // Default constructor (no-argument constructor)
    public PersonBean() {
    }

    // Getter method for 'name'
    public String getName() {
        return name;
    }

    // Setter method for 'name'
    public void setName(String name) {
        this.name = name;
    }

    // Getter method for 'age'
    public int getAge() {
        return age;
    }

    // Setter method for 'age'
    public void setAge(int age) {
        this.age = age;
    }
}

POJO (Plain Old Java Object) Example: 

A POJO, on the other hand, is a simple Java class that encapsulates fields and provides accessors and mutators but doesn't necessarily follow the additional conventions of a Java Bean, such as implementing Serializable or having a default constructor. 

Here's an example of a POJO:
public class Car {
    private String make;
    private String model;
    private int year;

    // Parameterized constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Getter method for 'make'
    public String getMake() {
        return make;
    }

    // Setter method for 'make'
    public void setMake(String make) {
        this.make = make;
    }

    // Getter method for 'model'
    public String getModel() {
        return model;
    }

    // Setter method for 'model'
    public void setModel(String model) {
        this.model = model;
    }

    // Getter method for 'year'
    public int getYear() {
        return year;
    }

    // Setter method for 'year'
    public void setYear(int year) {
        this.year = year;
    }
}



Comments