POJO vs Java Beans

From GeeksforGeeks

source link

POJO

POJO (Plain Old Java Object) is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath.

POJOs are used for increasing the readability and re-usability of a program. POJOs have gained the most acceptance because they are easy to write and understand. They were introduced in EJB 3.0 by Sun microsystems.

A POJO should not:

  1. Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { … } is not a POJO class.

  2. Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { … } is not a POJO class.

  3. Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { … } is not a POJO class.

[Doubt] 这里看起来和下文有矛盾,POJO 是可以继承空接口(比如 serializable)的,"prespecified" interfaces 应该是指框架接口,或者非空的接口

POJOs basically define an entity. Like in your program, if you want an Employee class, then you can create a POJO as follows:

public class Employee {
    String name;
    public String id;
    private double salary;
    public Employee(String name, String id, double salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public Double getSalary() {
        return salary;
    }
}

As you can see, there is no restriction on access-modifiers of fields. They can be private, default, protected, or public. It is also not necessary to include any constructor in it.

Java Beans

Beans are special type of Pojos. There are some restrictions on POJO to be a bean.

  1. JavaBeans is a proper subset of POJOs.

  2. Serializable i.e. they should implement Serializable interface. Still, some POJOs who don’t implement a Serializable interface are called POJOs because Serializable is a marker interface and therefore not of many burdens.

  3. Fields should be private. This is to provide complete control on fields.

  4. Fields should have getters or setters or both.

  5. A no-arg constructor should be there in a bean.

  6. Fields are accessed only by constructor or getter setters.

public class Bean {
    // private field
    private Integer property;

    // No-arg constructor
    Bean() {
    }

    // getter method for property
    public void setProperty(Integer property) {
        if (property == 0) {
            return;
        }
        this.property = property;
    }

    // setter method for property
    public Integer getProperty() {
        if (property == 0) {
            return null;
        }
        return this.property;
    }
}J

From GeeksforGeeks, the Serializable interface is present in java.io package. It is a marker interface, that is, an empty interface (no field or methods). Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized. Serialization is a mechanism of converting the state of an object into a byte stream. Serialization is done using ObjectOutputStream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. Deserialization is done using ObjectInputStream. Thus it can be used to make an eligible for saving its state into a file.

Last updated