What is POJO class?
Plain Old Java Object is called the POJO class. POJO class has some points are:
• The java class without any specialities i.e., it is a regular java class.
• While developing this class we need not follow any serious rules.
• The java class that is not extending from the technology or framework API class and does not implement technology or framework API interfaces is called the POJO class.
• Use of this class is improving the readability and reusability of the code.
• There is no restriction of modifiers of the properties in this class.
• This class is not mandatory for the 0-param constructor.
• This class is using getter() and setter() methods for transmitting and retrieving data from one layer to another in a real-time project.
• The POJO class should always be public.
• The POJO class should have the default constructor.
• The POJO class may contain the parameterized constructor if needed.
• Every Java Bean class is a POJO class.
Example:
class Test {
//your statement
}
→ It is a Pojo class
class Test implements Serializable {
//your statement
}
→ It is also a Pojo class
class Test extends Demo {
//your statement
}
class Demo {
//your statement
}
→ Test, Demo classes are POJO classes
class Test extends Thread {
//your statement
}
→ “Test” is a Pojo class
class Test extends HttpServlet {
//your statement
}
→ “Test” is not a Pojo class
class Test extends java.sql.Connection {
//your statement
}
→ “Test” is not a Pojo class
Create a POJO class:
//Person POJO class represents properties of the Person
public class Person{
//default property
Long id;
//public property
public String name;
//private property
private Integer mobileNo;
//parameterized constructor to initialize properties
public Person(Long id,String name,Integer mobileNo){
this.id=id;
this.name=name;
this.mobileNo=mobileNo;
}
//getter of the id property
public Long getId() {
return id;
}
//setter of the id property
public void setId(Long id) {
this.id = id;
}
//getter of the name property
public String getName() {
return name;
}
//setter of the name property
public void setName(String name) {
this.name = name;
}
//getter of the mobileNo property
public Integer getMobileNo() {
return mobileNo;
}
//getter of the mobileNo property
public void setMobileNo(Integer mobileNo) {
this.mobileNo = mobileNo;
}
}
→ In the above example we created a POJO class with the name “Person”. Properties of this class id, name, and mobileNo and modifiers of these properties are default, public and private respectively. This class contains parameterized constructor to initialize these properties. This class is also having the setter and getter method for each property for sending and retrieving data from classes. This is an example of a POJO class that uses every property in this Person class to make a POJO class.
→ We can use the POJO class as a Model class when we are working on a real-time project. At that time we are sending data from Controller to Service to DAO and vice versa. POJO class helps to use reusability features in the code in real-world scenarios.
→ We can use a private modifier for the properties of the POJO class when we are working on real-time projects.
Conclusion:
This topic is explained What is a Pojo class? How can we identify which is the POJO class?
Leave your thought here
Your email address will not be published. Required fields are marked *