POJO vs JavaBeans vs DTO vs VO
In this article, we will understand the difference Between POJO, JavaBeans, DTO and VO with their examples.
POJO stands for “Plain Old Java Object”.
It is a Java object that does not extend or implement any specific classes or interfaces.
A POJO typically only contains fields or instance variables and their getter/setter methods.
POJOs are also known as model or domain objects, and they are often used to represent the data in a database or applications.
If you are familiar with frameworks such as Hibernate, then an entity class is a POJO.
They are simple, easy to understand and maintain, and can be easily serialized and deserialized for data transfer.
POJOs are typically used as a plain data holder, it contains the state and behavior of the object, but it does not have any special methods or annotations that are required by frameworks.
Below is an example of a POJO class
public class Student { private int rollNo; private String name; /* * Constructor */ public Employee(int rollNo, String name) { this.rollNo = rollNo; this.name = name; } // getter and setter methods }
Java Beans
JavaBeans are similar to POJOs, but they follow a specific naming convention for the fields and methods.
Following are the main characteristics of a JavaBean are:
- It should have a default or no-arg constructor.
- Its fields or instance variables should be
private
and must have public getter and setter methods, following the naming convention ofget
andset
prefixes. - Field names must start with a small letter and should be in camel case.
- It should be serializable, that is, it must implement
java.io.Serializable
interface. - It should be mutable (i.e., its state can change after it is created).
Example of a java bean is
public class Student implements Serializable { private static final long serialVersionUID = 4549583243536089684L; private int rollNo; private String name; public Student() {} // getter and setter methods }
DTO stands for “Data Transfer Object”.
It is a POJO that is used to transfer data between layers or systems such as between the front end(view) or back end of a web application.
It contains fields, getter/setter methods, and constructors. Since, it is used to transfer data, it must be serializable and deserializable.
A DTO does not contain any business logic, it only contains data. If you are familiar with Spring boot web MVC, then you must be using DTO classes to send data from controller to front end client.
Example of a DTO class is
public class StudentDTO { private int rollNo; private String name; public StudentDTO() {} public StudentDTO(int rollNo, String name) { this.rollNo = rollNo; this.name = name; } // getter and setter methods }
VO
A VO, know as Value Object is a POJO that represents a value, such as a monetary amount or a date range.
It typically contains fields, getter/setter methods, and constructors, but it also includes methods for comparing and manipulating the value.
Since VO objects may be used in comparisons, they must be immutable.
And by the same reason, they must also override hashCode() and equals() methods, though it is not a strict requirement but it is a good practice to do so.
public class StudentVO { private int rollNum; private String name; public StudentVO(int rollNum, String name) { this.rollNum = rollNum; this.name = name; } // getter and setter methods @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; StudentVO studentVO = (StudentVO) o; return (rollNum == studentVO.rollNum) && Objects.equals(name, studentVO.name); } @Override public int hashCode() { return Objects.hash(rollNum, name); } }