POJO vs Java Beans - GeeksforGeeks

29 Nov, 2021
POJO classes
POJO stands for Plain Old Java Object. It 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:
Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { ... } is not a POJO class.Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { ... } is not a POJO class.Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { ... } is not a POJO class.
Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { ... } is not a POJO class.
Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { ... } is not a POJO class.
Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { ... } is not a POJO class.
POJOs basically define an entity. Like in your program, if you want an Employee class, then you can create a POJO as follows:
Java
// Employee POJO class to represent entity Employeepublic class Employee{    // default field    String name;      // public field    public String id;      // private salary    private double salary;      //arg-constructor to initialize fields    public Employee(String name, String id,                              double salary)    {        this.name = name;        this.id = id;        this.salary = salary;    }      // getter method for name    public String getName()    {        return name;    }      // getter method for id    public String getId()    {        return id;    }      // getter method for salary    public Double getSalary()    {        return salary;    }}
The above example is a well-defined example of the POJO class. 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.POJO is an object which encapsulates Business Logic. The following image shows a working example of the POJO class. Controllers interact with your business logic which in turn interact with POJO to access the database. In this example, a database entity is represented by POJO. This POJO has the same members as the database entity.
Java Beans
Beans are special type of Pojos. There are some restrictions on POJO to be a bean.
All JavaBeans are POJOs but not all POJOs are JavaBeans.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.Fields should be private. This is to provide complete control on fields.Fields should have getters or setters or both.A no-arg constructor should be there in a bean.Fields are accessed only by constructor or getter setters.
All JavaBeans are POJOs but not all POJOs are JavaBeans.
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.
Fields should be private. This is to provide complete control on fields.
Fields should have getters or setters or both.
A no-arg constructor should be there in a bean.
Fields are accessed only by constructor or getter setters.
Getters and Setters have some special names depending on field name. For example, if field name is someProperty then its getter preferably will be:
public "returnType" getSomeProperty()
{
   return someProperty;
} 
and setter will be
public void setSomePRoperty(someProperty)
{
   this.someProperty=someProperty;
}
Visibility of getters and setters is generally public. Getters and setters provide the complete restriction on fields. e.g. consider below the property,
Integer age;
If you set visibility of age to the public, then any object can use this. Suppose you want that age can’t be 0. In that case, you can’t have control. Any object can set it 0. But by using the setter method, you have control. You can have a condition in your setter method. Similarly, for the getter method if you want that if your age is 0 then it should return null, you can achieve this by using the getter method as in the following example:
Java
// Java program to illustrate JavaBeans class Bean {     // private field property     private Integer property;     Bean()     {         // No-arg constructor     }         // setter method for property     public void setProperty(Integer property)     {         if (property == 0)         {             // if property is 0 return             return;         }         this.property=property;     }         // getter method for property     public Integer getProperty()     {         if (property == 0)         {             // if property is 0 return null             return null;         }         return property;     } }     // Class to test above bean public class GFG {     public static void main(String[] args)     {         Bean bean = new Bean();             bean.setProperty(0);         System.out.println("After setting to 0: " +                                  bean.getProperty());             bean.setProperty(5);         System.out.println("After setting to valid" +                       " value: " + bean.getProperty());     } }
Output:- 
After setting to 0: null
After setting to valid value: 5
POJO vs Java Bean
POJO
Java Bean
Conclusion
POJO classes and Beans both are used to define java objects to increase their readability and reusability. POJOs don’t have other restrictions while beans are special POJOs with some restrictions.
This article is contributed by Vishal Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
rutvikatGFG
PHANIX
Sanan07
Ameya Gharpure
anilkumarpal1
akshaysingh98088
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Initialize an ArrayList in Java
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
LinkedList in Java