What are POJO classes in spring? What is the use of POJO Class, and How to create POJO classes.

TCS Interview Question  3+

What are POJO classes in spring? What is the use of POJO Class, and How to create POJO classes.

POJO -  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 class path. POJO's are used for increasing the readability and re-usability of a program.

=> The term POJO was introduced by Martin Flower (An American Software developer) in 2000. It is available in JAVA from the EJB 3.0 by sun microsystem.

Generally, a POJO Class contains Variables and their Setters & Getters.

=> The POJO Classes Similar to beans as both are used to define the Objects to increase the readability and reusability .

=> The only difference between them ,that Bean Files have some restrictions but, the POJO files do not have any Special restrictions.


What is the use of  POJO Classes:-

=> The POJO Class in JAVA is used to implement readability & reusability. The POJO class in JAVA is an Object Class that encapsulates the business logic. The POJO Class fields can be public, private or protected. JavaBeans are Classes that encapsulate Many Objects into a Single Object.

Example of POJO Class :-

POJO Class is used to define the object entities. For Example, we can Create an Employee POJO class to define its Objects

Employee.java

package p1;
// Pojo Class example

public class Employe {
private String name;
private String id;
private double sal;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}

}

********************************************************************

Properties of POJO :-

Below are some Properties of the POJO Class:-

1. The POJO class must be public.
2. It must have a public default Constructor.
3.A POJO Class should not extend Predefined Classes.
4.It should not implement prespecified interfaces.
5.It should not have any prespecified annotation.

**********************************************************************

Working of POJO Class :-

The POJO Class is an Object Class that encapsulates the Busyness logic. In MVC architecture, the Controller interacts with the business logic, Which Contacts with POJO Class to access the data.

Diagram:-

How to use POJO Class in JAVA Program:-

The POJO Class is Created to we the Object in other JAVA Programs. The major advantage of the POJO class is that we will not have to Create Objects every time in  other JAVA Programs. 
Simply we Can access the Objects by using the get()  and set() methods.

Example:-

package p1;

public class PojoExample {

public static void main(String[] args) {
//Create Employee class object
Employe obj=new Employe();
obj.setName("Alisha");
obj.setId("A001");
obj.setSal(200000);
System.out.println("Name:"+obj.getName());
System.out.println("Id:"+obj.getId());
System.out.println("Salary:"+obj.getSal());

}

}
**************************************************************************


 




Comments

Popular posts from this blog

How to create React project using command prompt?