I have seen a lot of similarity between these two concepts. They both have protected attributes and simple classes. But I wish I knew what their difference is
I have seen a lot of similarity between these two concepts. They both have protected attributes and simple classes. But I wish I knew what their difference is
POJO
POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restrictions that are not enforced by the Java language specification, and does not require a classpath. POJOs are used to increase the readability and reusability of a program. POJOs have gained the most acceptance because they are easy to write and easy to understand. They were introduced in EJB 3.0 by Sun Microsystems.
A POJO must not contain the following:
Extend prespecified classes, Ex:
public class GFG extends javax.servlet.http.HttpServlet {...}
not a POJO class.Implement prespecified interfaces, eg
public class Bar implements javax.ejb.EntityBean {...}
not a POJO class.Prespecified annotations, eg
@javax.persistence.Entity public class Baz {...}
not a POJO class.POJOs basically define an entity. If you want an employee class in your program, then you can create a POJO like this:
The example above is a well-defined example of the POJO class. As you can see, there is no restriction on the access-modifier of the fields. They can be private, default, protected, or public. You also don't need to include any constructors in it.
POJO is an object that encapsulates business logic. The following image shows a practical example of the POJO class. Controllers interact with your business logic, which in turn interact with POJOs to access the database. In this example, a database entity is represented by POJO. This POJO has the same members as the database entity.
BEANS
Beans are special types of POJOS. There are some restrictions on POJOs so they can be Beans.
Getters and Setters have some special names depending on the name of the field. For example, if the field name is someProperty then its getter will preferably be:
and the setter will be called:
The visibility of getters and setters is generally public. Getters and setters provide complete restriction on fields.
If you set the age visibility to public, then any object can use it. Suppose you want the age to not be 0. In that case, you can't have control. Any object can set it to 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 if your age is 0 then it should return null, you can achieve this using the getter method as in the following example:
Result:
CONCLUSION
POJOs and Beans classes are used to define Java objects to increase their readability and reusability. POJOs have no other restrictions while Beans are special POJOs with some restrictions.
Source: POJO vs Java Beans (see an interesting comparative table at the end).
The only notable differences are that a Java Bean can be serialized (although it's not in the spec) and a POJO can only inherit from an Object.
POJO :
Bean (JavaBean) :
Example:
I suggest you review this document for more information: "Difference Java Bean vs POJO" and the JavaBeans specification .
POJO definition (Wikipedia):
JavaBean definition (Wikipedia):
A bean in Java is defined in the JavaBeans specification :
Translated into Spanish:
More elements are defined for a bean such as the nomenclature of attributes and methods, access methods to attributes, support for events, among others.
POJO stands for Plain Old Java Object. This refers to a class with private or protected attributes and that only provides protected or public getter and setter methods to access the attributes. Similar to when you define an entity. A POJO is a JavaBean while a JavaBean is not necessarily a POJO.